This question already has answers here:
Getting a Custom Objects properties by string var [duplicate]
(2 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 months ago.
I have an array retrieved from a JSON file and I am using dataArray.forEach(function(e, idx, array) to loop every element and create a table.
I can simply call for each element with console.log(e.id) but how can I list properties with incremental numbers on them without specifying it or its exact the position in loop?
I need to end up with something like this console.log(e.dp_1.value); but instead of this number I wish to use idx. Is this possible?
I have heard there is getChildByName but not sure how to use it and if it fits.
{
"devices": [{
"id": 0,
"sn": "#12BB56CC9",
"dp_1": {
"value": "65345",
"multiplier": 0.001,
"digits": 2
},
"dp_2": {
"value": "55345",
"multiplier": 0.001,
"digits": 2
}
},
// much more like those
]
}
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I'm not able to get the value by dynamic key in following code,
const data = {
"Id": "1234",
"status": "open",
"Translations": {
"EN": "English",
"ES": "Spanish",
"FR": "French"
}
};
const translationKey = "FR";
console.log(data.Translations.translationKey)
How can I get "French" text from the object, If translationKey is dynamic ?
Any help is much appreciated.
Use bracket notation if you want to use a variable property name data.Translations[translationKey]. Here you will find more details.
It's the same notation one uses for accessing array elements, since an array is also an object.
This question already has answers here:
How to efficiently count the number of keys/properties of an object in JavaScript
(19 answers)
Closed 3 years ago.
I have an data object in a state, like so:
this.state = {
data: {
newsData: [],
eventData: [],
},
}
How can I get the number of properties defined in the state data? data.length doesn't work since it's not an array.
You can use Object.values(data).length
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I am trying to access a nested object based on the name of the object
I have already tried what I did below but it is giving me an error saying that "Cannot read property 'amount' of undefined" Can someone please explain to me why this doesn't work to point me in the direction of an alternative method? Thanks
var test = {
"name": {
"amount": "200"
},
"telle": {
"amount": "150"
}
}
function getIt(testing) {
return test.testing.amount;
//return test.name.amount;
}
console.log(getIt("name"))
function getIt(testing) {
return test[testing].amount;
}
That way you tell js to look for key with name stored inside testing variable.
This question already has answers here:
Add new attribute (element) to JSON object using JavaScript
(11 answers)
Add property to each object in the array [duplicate]
(2 answers)
Closed 6 years ago.
I have a JSON object of the following format
[{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"},
{......},
{......},
]
I need to add a new attribute to each row based on some calculations.
Expected result
[{"field1":1,"field2":"A","field3":"B","field4"="generatedVal1"},
{"field1":2,"field2":"B","field3":"C","field4"="generatedVal2"},
{......},
{......},
]
How can I achieve this using javascript?
Use Array.prototype.forEach method:
[
{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"}
]
.forEach(obj => {
obj.field4 = 'Something'
})
Sidenote on terminology: you don't have any JSON, you have javascript array (object). JSON is a string representation of this object, but in your question you are talking about object itself.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have an array and want to access its data. However, I need to use a variable instead of the name to access the data.
For example;
My data:
$scope.myData = {
"user": [
{ child[{......}],
..........
}
],"user2": [
{
child[{......}],
.........
}
],...........
The following works
console.log("lenght:"+$scope.myData.user[0].child.length);
but I want to use a variable instead of user[0], because it is dynamic, it changes every time.
Similar to
var m=user;
console.log("lenght:"+$scope.myData.m[0].child.length);
How about this?
var m = 'user';
console.log("lenght:"+ $scope.myData[m][0].child.length );