Answer the question
In order to leave comments, you need to log in
How to find an object with a property that has a certain value in a structure with unknown nesting?
It is necessary to find and get an object in which one of the properties will be equal to the search string.
For example, there is an array:
let arrayData = [
{
value: 'test',
newArray: [
{
value: 'zero',
newArray: [
{
value: 'myValue'
}
]
}
]
}
]
Answer the question
In order to leave comments, you need to log in
function find(obj, val) {
const values = obj instanceof Object ? Object.values(obj) : [];
return values.includes(val)
? obj
: values.reduce((found, n) => found || find(n, val), null);
}
const obj = find(arrayData, 'myValue');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question