4

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
 }]
Jack Bashford
44.3k11 gold badges56 silver badges83 bronze badges
asked Apr 4, 2019 at 3:39

3 Answers 3

2

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)

answered Apr 4, 2019 at 4:09

Comments

1

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"));

answered Apr 4, 2019 at 3:47

Comments

1

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

answered Apr 4, 2019 at 3:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.