Accessing object of objects - JSON API [duplicate] - javascript

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

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

Object complexity while search by key [duplicate]

This question already has answers here:
Complexity of accessing data in an object
(2 answers)
Closed 1 year ago.
I have the following object:
let obj = {
"name": "someName",
"age": 33,
"city": "someCity"
}
I understand that an object is not indexed
"JS objects have no defined order, they are (by definition) an
unsorted set of key-value pairs."
so what is the complexity of:
let result = obj.name
Is it searching all over the object to find the right key?
Is there a way to index this object so complexity will be O(1)?
Data access which involves retrieving or modifying data stored in an object is done in constant time O(1). This is also true for the insertion and removal of data.

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

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.

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.

create JSON object for jsTree from XML data [duplicate]

This question already has an answer here:
Why is my Array behaving differently outside of an AJAX function? (populating jsTree) [duplicate]
(1 answer)
Closed 5 years ago.
I can't populate jsTree because something is wrong with the array that I'm creating.
jsTree allows you to pass in JSON data, so I'm trying to format an array of objects that jsTree will like:
var myAry = [];
$(xml).find('group').each(function() {
myAry.push({
"id": $(this).find('GroupID').text(),
"parent": "#",
"text": $(this).find('GroupName').text(),
});
});
When I dump [myAry] to the console, it looks like a properly formatted Array, but jsTree doesn't like it. However, if I create an array manually, jsTree likes it:
var testAry = [
{"id": "42", "parent": "#", "text": "Foo"},
{"id": "69", "parent": "#", "text": "Bar"},
{"id": "1", "parent": "#", "text": "Dolphin"},
];
What's going wrong in my loop?
This may help (from jQuery docs):
The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object.
The method you're using accepts a selector argument, but you're passing it the name of a JSON object.
Try using $.each().

Categories