Computer Science, asked by jvpalmadiaz9550, 1 year ago

Find the object with key in a nested array of objects in javascript

Answers

Answered by Anonymous
0

Answer:

account for property arrays:

function getObject(theObject) {

var result = null;

if(theObject instanceof Array) {

for(var i = 0; i < theObject.length; i++) {

result = getObject(theObject[i]);

if (result) {

break;

}

}

}

else

{

for(var prop in theObject) {

console.log(prop + ': ' + theObject[prop]);

if(prop == 'id') {

if(theObject[prop] == 1) {

return theObject;

}

}

if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {

result = getObject(theObject[prop]);

if (result) {

break;

}

}

}

}

return result;

}

Answered by Radhaisback2434
0

Explanation:

To find an object property by key deep in a nested JavaScript array, we can traverse the deeply nested array with various loops and methods.

Hope its help..

Similar questions