As mentioned in the title, I need to get single object values from an array which contains many (nested) objects and save these values in a new array. This works fine with map like:
var newArrayA = array.map(x => x.PI[0].ZZ);
var newArrayB = array.map(x => x.FS.CC.FR);
var newArrayC = array.map(x => x.FS.BD.FR);
In case all objects contain all values as you can see here:
var array =
[
{ _id: 'MEuDZjQDw8XQk4ny7Y',
PI: [
{ZZ: "ABC"}
],
FS:
{ CC: {FR: 1},
BD: {FR: 2}
}
},
{ _id: 'gFsya9tHMAkAoPgvw',
PI: [
{ZZ: "DEF"}
],
FS:
{ CC: {FR: 3},
BD: {FR: 4}
}
}
]
There could be many more 'id' objects as well as more fields within the data structure of the array. However, I receive an error if for example the second object does not contain a FS field:
var array =
[
{ _id: 'MEuDZjQDw8XQk4ny7Y',
PI: [
{ZZ: "ABC"}
],
FS:
{ CC: {FR: 1},
BD: {FR: 2}
}
},
{ _id: 'gFsya9tHMAkAoPgvw',
PI: [
{ZZ: "DEF"}
]
}
]
The problem is that I still need to get the FS.CC or FS.BD values from the objects which contain these values. It would be perfectly fine if I get a "empty or "undefined" in case the values are not in an object and the values if they are in the object:
var neededNewArrayC = [2, "empty"];
Or is there maybe another solution? I would appreciate any help! Thanks.
Aucun commentaire:
Enregistrer un commentaire