📌  相关文章
📜  查找带有子元素的数组 javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:36.859000             🧑  作者: Mango

代码示例1
const find = (array = [], id) => {
  for (const item of array) {
    const result = item.id === id ? item : find(item.children, id);
    if(result) return result;
  }
};

const 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' }] }];

const result = find(commentList, 15);

console.log(result);