Reputation: 405
I have an example of javascript array of object within an object there is object. So I basically want only that into one dimensional array.
I have searched lots of Q's on SO but none of them as per my requirement.
What I have
[
{
"id": "59cf758f7bdf8d2e0c1c68c7",
"value": {
"city": "Mahbubnagar",
"state": "Andhra Pradesh",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68c8",
"value": {
"city": "Udgir",
"state": "Maharashtra",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68c9",
"value": {
"city": "Umarga",
"state": "Maharashtra",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68ca",
"value": {
"city": "Umarkhed",
"state": "Maharashtra",
"country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68cb",
"value": {
"city": "Umred",
"state": "Maharashtra",
"country": "India"
}
}
]
And I simply want this array of objects into an array
[
"59cf758f7bdf8d2e0c1c68c7",
"Mahbubnagar",
"Andhra Pradesh",
"India",
],[
"59cf758f7bdf8d2e0c1c68c9",
"Umarga",
"Maharashtra",
"India",
],[
"59cf758f7bdf8d2e0c1c68ca",
"Umarkhed",
"Maharashtra",
"India",
],[
"59cf758f7bdf8d2e0c1c68c7",
"Mahbubnagar",
"Andhra Pradesh",
"India",
],
The array of objects could be have more children in other words you can say its dynamic content.
What I have used so far is
var object= property.value.elements;
var finalArray = object.map(function (obj) {
return obj.id;
});
console.log(finalArray);
Which is giving me only list of Id's
It's not duplicate question please verify before marking it as Duplicate.
Upvotes: 0
Views: 1235
Reputation: 4354
This isnt the easiest way to do it. Just loop over all the objects and extract their values.
let x = [{"id": "59cf758f7bdf8d2e0c1c68c7", "value": {...}, ..]
let res = []
x.forEach((obj, index) => {
res.push([obj.id, obj.value.city, obj.value.state, obj.value.country])
})
console.log(res)
Upvotes: 0
Reputation: 386540
You could check if a value of the object is an object an take the flattened valued for the result.
function flat(object) {
return Object
.values(object)
.reduce((r, v) => r.concat(v && typeof v === 'object' ? flat(v) : v), []);
}
var array = [{ id: "59cf758f7bdf8d2e0c1c68c7", value: { city: "Mahbubnagar", state: "Andhra Pradesh", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68c8", value: { city: "Udgir", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68c9", value: { city: "Umarga", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68ca", value: { city: "Umarkhed", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68cb", value: { city: "Umred", state: "Maharashtra", country: "India" } }],
result = array.map(flat);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 28445
Try following
var arr = [{"id":"59cf758f7bdf8d2e0c1c68c7","value":{"city":"Mahbubnagar","state":"Andhra Pradesh","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68c8","value":{"city":"Udgir","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68c9","value":{"city":"Umarga","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68ca","value":{"city":"Umarkhed","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68cb","value":{"city":"Umred","state":"Maharashtra","country":"India"}}];
function getVals(obj, arr) {
Object.entries(obj).forEach(([key, value]) => {
if(typeof value === "object") {
arr = [...arr, ...getVals(obj[key], arr)];
} else {
arr.push(value);
}
});
return arr;
}
var resp = arr.map((item) => {
let temp = [];
getVals(item, temp);
return temp;
});
console.log(resp);
Upvotes: 0