Assign dynamic index in array of object in Angularjs [duplicate] - javascript

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
If I have array of object in such a way.
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
Using above I want to assign Prashant in $scope.getColumn[i].value in dynamic way. I have try like this
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
but this give me Uncaught SyntaxError: Unexpected token +
I can get Prashant Shukla by data[0].name but How can I get exactly same by using $scope.getColumn[i].Field in place of name index ?
how can I resolve this error and assign value dynamically?
thanks ahead.

you can use as like as given below:
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
console.log( data[0][ $scope.getColumn[i].Field ] ); // 'Prashant Shukla'

$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
Why you have . after the data[0]?
It should be data[0].name +$scope.getColumn[i].Field;

Related

ReactJS : How can get value in object [duplicate]

This question already has answers here:
Get array of object's keys
(8 answers)
Closed 3 months ago.
Object Im have Object , but how can get keys + value in this (main : , weather : , clouds: , cors : , ... )
Im try use map but can't go next entry
My demo
Sorry im bad english . thanks alot
Please, see here. This is how you get both the key and value of an object.
const foobar = {main: "hello", clouds = "world", cors: 1}
const keypairs = Object.entries(foobar);
//looping
keypairs.map(([key, value]) => { // do something });
just replace value with value.main/ value.weather and it will work
andd one thing cities.map() is not enough you dont need cities.list.map()
good luck!

Issue getting my objects properties [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
Hi guys I have just started learning to code and I have hit a road block in accessing the properties of the object I have created.
Here is my object.
var restaurantOrder = {
"my entree": "cheeseburger",
"my side": "fries",
"the drink": "water"
};
I would like to get the value of entree however nothing I am trying seems to work :(
Here is what I have tried.
var entreeValue = restaurantOrder.my entree;
var entreeValue = restaurantOrder[my entree];
var entreeValue = restaurantOrder.[my entree];
var entreeValue = restaurantOrder.["my entree"];
None of the above lines work :( Thank you for your help.
Because the properties of your restaurantOrder object have spaces, you will need to use [] to access them. You cannot use . like you can with a property name that is a single word.
Also, because of the spaces you will need to enclose the property name with quotes, so either:
var entreeValue = restaurantOrder["my entree"];
or
var entreeValue = restaurantOrder['my entree'];
will work.

How do I use a variable to find an element in an array? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'd like to take some user input and use it to find a certain object in an array. But when I try to do it with the code below, I get an undefined error. What am I doing wrong?
function findNextLevel() {
var currentLevel = parseFloat(document.getElementById("currentLevel").value);
var xpForLevel = trainerLevels.currentLevel;
document.getElementById("result01").innerHTML = xpForLevel;
}
I'm assuming that trainerLevels is an array and currentLevel is an index. If so, the way to access an element in an array at a certain index is to use brackets like so. Otherwise, could you provide more details in your question?
var xpForLevel = trainerLevels[currentLevel];
If this is the answer that you were looking for, then may I recommend that you use the parseInt rather than the parseFloat function for getting the index? And also, since it is user input, you may want to check that currentLevel is in the correct range as well.

cannot access property - JS Error Shown [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
My Code to access a property:
var myArray = [{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124070","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""},{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124072","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""}];
alert(myArray[2].Order# );
<html>
<body>
</body>
</html>
Getting an JS Error Uncaught SyntaxError: Unexpected string ->on alert - when I code as .Order# and if I use .Order then value is undefined.
what could be the possible solution ??
You have to use the bracket notation : json[2]['Order#']
You can't use # out of a string
var myArray = [{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124070","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""},{"td_stlmnt":"NN2015227","Heading":"NN2015227[02/12/2015]","td_scripcd":"514286","Order#":"1000000003042299","Trade#":"1124072","Time":"14:40:38","Security":"ASHIMASYN (514286)","Buy":"250","Sell":"0","Market Rate":"12.90","Brokerage":"12.50","Buy Value":"3237.5000","Sell Value":".0000","_":"","Ordr":"ASHIMASYNb","BDate":"20151202","Net Value":""}];
document.write(myArray[1]['Order#']);

How to escape # in javascript to retrive object property [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
i have Javascript object as below.
How can i get value for SERIAL#, like i can retrieve *.INST_ID or *.INSTANCE.
how can i escape # and get the value required.
So far i have tried SERIAL#23% but none helped so far.
var t = {"INST_ID":"1","INSTANCE":"xina","SID":"27","SERIAL#":"48810", "PROGRAM":"Perl#app01"}
console.log(kSess.SERIAL%23); gives syntax error
I am parsing variable "t"
This data is coming from java code, so there is nothing much i can do to change SERIAL# to something else
Any idea?
Try to retrieve the value like this...
t["SERIAL#"]; // will return you the value..
Store it somewhere or play with it as you like. :)

Categories

Resources