Javascript get value based on dynamic key in object [duplicate] - javascript

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.

Related

How to extend variable name with a number? [duplicate]

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
]
}

how to update nested object in Firebase firestore DB [duplicate]

This question already has answers here:
Cloud Firestore: Update fields in nested objects with dynamic key
(7 answers)
Firestore: Update fields in nested objects
(1 answer)
Update fields in nested objects in firestore documents?
(9 answers)
Update a field in an object in Firestore?
(2 answers)
Closed 2 years ago.
I'm trying to update a nested object in the firestore DB I'm using the web SDK. Here is the format of the object that I wish to update:
{
"name": "Math",
"sections": {
"section1": {
"sectionName": "Introduction",
"lessonsObj": {
"1": {
"pageResource": "page resource",
"title": "Intro"
}
}
}
},
"description": "Lorem ipsum."
}
I'd like to update the title property for the first lesson in the lessonsObj object. The values for section1, and the lesson key number 1 will be dynamic, but I'm not sure how I can do this?
Here is the screenshot of the structure in FB Firestore:
You can use dot notation to build a path to a field that is nested in maps, as described in the documentation.
document.update({
"JavaScript.sections.section1.1.title": "value"
})
You will need to provide the full path as a string. You can build that string any way you want, using variables as necessary. You will just need to be able to call out the full name of the value.
This will not work if any of the nested objects is an array.

Javascript loop through object keys [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I have the following javascript object
const items = {
"item 1":{name:'test',vals:[{name:'first',....}]},
"item 2":{name:'test3',vals:[{name:'second', ....}]}
//other keys
}
The object keys are dynamic and not just "item 1" and 2 and they keep on changing. I want to access the name property of each object. SO i have done the following by am stuck
Object.keys(items).forEach(key=>{
let keyitem = items.key //am stuck on how to pass the key
})
How do i proceed to the dynamic key as the items.key is undefined as there is no such key in the items object.How do i proceed.
Replace items.key as items[key]
Object.keys(items).forEach(key=>{
let keyitem = items[key]
})

Nested Objects and function variables [duplicate]

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.

Accessing object of objects - JSON API [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm wonder how to properly access to object of objects.I have JSON API, and I have to get some data from It.
FYI I'm using VueJS but I think that's not important thing here.
{
"name": "Foo Bar",
"details": {
"color": "black",
"slug": "foobar",
"author": "John",
"material": "Plastic"
}
}
How could I access for e.g to the slug ? Those data are stored into parent object called product (VueJS Dev Tools)
var slug = product.details.slug

Categories