Let's suppose I have an array like this:
const arr = [
[
[
{name: 'Bob'},
{name: 'John'},
],
[
{name: 'Maria'},
{name: 'Marin'},
{name: 'Marix'},
{name: 'Alex'}
],
],
[
[
{name: 'JP'},
{name: 'Dox'},
{name: 'Dor'},
{name: 'Dog'},
],
[
{name: 'Dol'},
{name: 'Fol'},
{name: 'Fol'},
{name: 'Fol'}
],
]
];
I have a name and I want to find the index on the object in the array.
There is my solution it works but I want to find another solution without 3 forEach.
const givenName = 'Dox';
let myIndex;
arr.forEach((firstDepth) => {
if (firstDepth && Array.isArray(firstDepth)) {
firstDepth.forEach((secondDepth, i) => {
secondDepth.forEach((lastStep) => {
if (lastStep) {
const { name } = lastStep;
if (name === givenName) {
myIndex = i;
}
}
});
});
}
});
Thanks.