Find index of array with array of objects

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Find index of array with array of objects on this date .

I’ve found some good answers about how getting an index of an array with indexOf. However, this doesn’t suit me since I must return the the index of the parent array that contains a child array which contains an object with certain id.

This would be a sample array:

[
    [{id: 1}],
    [{id: 2}],
    [
        {id: 3}
        {id: 4}
    ],
    [
        {id: 5},
        {id: 6}
    ],
    [{id: 7}],
]

For example, if I’d like to find the index of the id : 4 the index would be 2.

I’ve thought about a map inside the findIndex method:

array.findIndex((el) => el.map((e) => e.id === noteId )));

But have not found much success yet.

Answer

As Andy Ray commented, a map doesnt return a true value.

so the correct way to do it is this:

array.findIndex((el) => el.some((e) => e.id === noteId )));

const array = [
    [{id: 1}],
    [{id: 2}],
    [
        {id: 3},
        {id: 4},
    ],
    [
        {id: 5},
        {id: 6}
    ],
    [{id: 7}],
]

const noteId = 3;

console.log(array.findIndex((el) => el.some((e) => e.id === noteId )));