const recursiveFindIndex = (data, id) =>
data.reduce((indexes, item, index) => {
let subIndex;
Array.isArray(item) && (subIndex = recursiveFindIndex(item, id));
if (subIndex && subIndex.length) {
return indexes.concat([index], subIndex);
}
item.id === id && (indexes.push(index));
return indexes;
}, []);
const data = [
{ id: 0 },
[{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }],
[{ id: 6 }, { id: 7 }, { id: 8 }, { id: 9 }, [
{ id: 10 }, { id: 11 }, { id: 12 }, { id: 13 }, { id: 14 }]
]
];
console.log('id: 3', recursiveFindIndex(data, 0));
console.log('id: 3', recursiveFindIndex(data, 3));
console.log('id: 6', recursiveFindIndex(data, 8));
console.log('id: 12', recursiveFindIndex(data, 12));
console.log('id: 3', recursiveFindIndex(data, 20));