Reputation: 255
I would like to know how to iterate through a nested array of objects in Javascipt? I have a sample object named obj. I want to retrieve the object based on condition 'in' is 'string' and 'out' 'number'.
// tried got stuck
const output = [];
myList.forEach(entry => {
Object.keys(entry).forEach(key => {
const entity = entry[key][0];
if (entity.in === "string" && entity.out === "number") {
output.push(entity);
}
});
});
var obj = [{
"ston": [{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
},{
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}],
"nton": [{
"id": "identity1",
"in": "number",
"out": "number",
"value": 20
},{
"id": "identity2",
"in": "number",
"out": "number",
"value": 30
}]
}]
Expected Output
[{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
},{
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}]
Upvotes: 4
Views: 3742
Reputation: 17190
Here you have one solution that uses mainly Array.reduce() to iterate over the outter objects of the array, get a flattened array of the values from every outter object to create an array with inner objects and then filter those meeting the condition while saving they in a new array:
var obj = [
{
"ston": [
{"id": "identity1", "in": "string", "out": "number", "value": 10},
{"id": "identity2", "in": "string", "out": "number", "value": 10}
],
"nton": [
{"id": "identity1", "in": "number", "out": "number", "value": 20},
{"id": "identity2", "in": "number", "out": "number", "value": 30}
]
}
];
let res = obj.reduce((acc, o) =>
{
acc = acc.concat(Object.values(o).flat().filter(
o => o.in === "string" && o.out === "number"
));
return acc;
}, []);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Other documentation of used methods:
Or, after some time thinking, you can use the next simplified version with Array.map()
var obj = [
{
"ston": [
{"id": "identity1", "in": "string", "out": "number", "value": 10},
{"id": "identity2", "in": "string", "out": "number", "value": 10}
],
"nton": [
{"id": "identity1", "in": "number", "out": "number", "value": 20},
{"id": "identity2", "in": "number", "out": "number", "value": 30}
]
}
];
let res = obj.map(Object.values).flat(2).filter(
o => o.in === "string" && o.out === "number"
);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Upvotes: 1
Reputation: 2032
You can use rebuild that object to a nested array then flatten and finally filter
.
var obj = [
{
"ston": [
{"id": "identity1", "in": "string", "out": "number", "value": 10},
{"id": "identity2", "in": "string", "out": "number", "value": 10}
],
"nton": [
{"id": "identity1", "in": "number", "out": "number", "value": 20},
{"id": "identity2", "in": "number", "out": "number", "value": 30}
]
}
];
const tmp = obj.map(e => Object.entries(e).map(([k, v]) => v)).flat(3)
const rs = tmp.filter(e => e.out==='number' && e.in ==='string')
console.log(rs)
Upvotes: 2
Reputation: 44087
Simple recursive function:
var obj = [{
"ston": [{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
}, {
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}],
"nton": [{
"id": "identity1",
"in": "number",
"out": "number",
"value": 20
}, {
"id": "identity2",
"in": "number",
"out": "number",
"value": 30
}]
}];
function getObjects(inVal, outVal) {
var matches = [];
obj.forEach(child => {
Object.values(child).forEach(arr => {
if (arr.some(e => e.in == inVal && e.out == outVal)) {
matches.push([...arr.filter(e => e => e.in == inVal && e.out == outVal)]);
}
});
});
return matches.flat();
}
console.log(getObjects("string", "number"));
Upvotes: 1