showing results for - "find array with children javascript"
Jacopo
01 Mar 2018
1const find = (array = [], id) => {
2  for (const item of array) {
3    const result = item.id === id ? item : find(item.children, id);
4    if(result) return result;
5  }
6};
7
8const commentList = [{ id: 1, text: 'A', children: [{ id: 2, text: 'B' }] }, { id: 4, text: 'asd', children: [] }, { id: 5, text: 'vx', children: [{ id: 7, text: 'xxss' }] }, { id: 8, text: 'ghfdh', children: [{ id: 15, text: 'I want to take this' }] }, { id: 10, text: 'A', children: [{ id: 18, text: 'Bsda' }] }];
9
10const result = find(commentList, 15);
11
12console.log(result);