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
}]
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
}]
Share
Improve this question
edited Apr 4, 2019 at 3:52
Jack Bashford
44.2k11 gold badges55 silver badges82 bronze badges
asked Apr 4, 2019 at 3:39
miamia
2651 gold badge5 silver badges17 bronze badges
3 Answers
Reset to default 2You 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)
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"));
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:
- Array.concat()
- Array.flat()
- Array.filter()
- Object.values()
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;}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613142a4636069.html
评论列表(0条)