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

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.

Related

How to add an object to an array that already contains objects without rewriting data in firestore? [duplicate]

This question already has answers here:
how to add objects to array in firestore?
(1 answer)
How can I add data to a firestore array without overwriting it's previous content?
(1 answer)
Closed 29 days ago.
im trying to insert an object in an array in firestore but when the array already contains an object it just rewrite it and i only get 1 object everytime.
here is my code
const obj=[{
name:xxx,
age:28
}];
await dataBase.set({infos:obj},{merge:true})
Here is what I'm trying to do.
To add an object to an array that already contains objects in Firestore without rewriting the entire array, you can use the arrayUnion() or arrayRemove() method.
Here is an example of using arrayUnion() to add an object to an array:
// Add a new object to the 'items' array
firestore.collection("collectionName").doc("docId").update({
items: firebase.firestore.FieldValue.arrayUnion({ name: "itemName",
quantity: 2 })
});

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

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.

Better mapping JSON to objects in JavaScript [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Access Javascript nested objects safely
(14 answers)
Test for existence of nested JavaScript object key
(64 answers)
Closed 5 years ago.
I'm using v4 sheets API from google-api-nodejs-client, it returns a lot of data. Though I filter it using fields arguments, the structure of JSON is very complex. So to incorporate it into my logic, I flatten it. And it needs to be safe, I don't want any exceptions to be thrown.
This is how the UNSAFE version of the code looks:
const data = response.sheets[0].data;
const columns = data.map((column) => {
const rowData = column.rowData;
const values = rowData[0].values;
return rowData.map((cellData) => cellData.values[0].userEnteredValue);
});
// ...
If JSON is invalid, an exception is inevitable. I don't want this. The problem is that to fix it, I should add plenty of if-s. This would make the code look ugly.
Let's summarize: Is there a way to map JSON into local objects safely and expressive (and preferably relying only on features of ES6)?
UPDATE
Let's add a little more context. This is a small insight how deep the JSON is, and all I need to map it to an array of strings ('1.06' is one of them).
{
"sheets":[
{
"data":[
{
"rowData":[
{
"values":[
{
"userEnteredValue":{
"stringValue":"1.06"
}
}
]
},
...

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