最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS: Find object by field in a complex Parent-Child Array - Stack Overflow

programmeradmin0浏览0评论

I use Javascript ES6 and have an issue.

I have an Array:

var 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' }] },
]

This is an Array with Parent - Child Structure. How can I get Exactly object {'id': 15, text: 'I want to take this' } if I just have ID only

I tried but not work

var object = commentList.find(o => o.id === 15) => undefined

I use Javascript ES6 and have an issue.

I have an Array:

var 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' }] },
]

This is an Array with Parent - Child Structure. How can I get Exactly object {'id': 15, text: 'I want to take this' } if I just have ID only

I tried but not work

var object = commentList.find(o => o.id === 15) => undefined

Share Improve this question edited Nov 1, 2019 at 22:49 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Nov 11, 2018 at 11:20 KitKitKitKit 9,53315 gold badges64 silver badges89 bronze badges 3
  • Can those children arrays be nested arbitrarily or is there only a single level? – Bergi Commented Nov 11, 2018 at 11:22
  • Look up recursion – Quentin Commented Nov 11, 2018 at 11:22
  • commentList.concat(...commentList.map(o => o.children)).find(o => o.id == 15) should do it – Bergi Commented Nov 11, 2018 at 11:23
Add a comment  | 

4 Answers 4

Reset to default 8

You could take an iterative and recursive approach by checking the id or taking the children.

const find = (array, id) => {
    var result;
    array.some(o => result = o.id === id ? o : find(o.children || [], id));
    return result;
};

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

console.log(find(commentList, 15));

You can use for...of to find the item recursively. If there's no item, the function will return undefined:

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);

I would extract the logic into a function and then iterate over the children array and return the one you requested (The first match).

var 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 findChildById = (id, arr) => {
  const result = arr.find(o => o.id === id)
  if (result) return result
  for (const cm of arr) {
    const result = cm.children.find(o => o.id === id)
    if (result) return result
  }
}
console.log(findChildById(10, commentList))

Following very simple and basic code should work for you. I assume that all id-s of all children elements in all arrays are unique. This code will find the first element that matches the id we are looking for;

var result = null;
var idToSearch = 15;
var i=0;
var j=0;

for(i=0; i<commentList.length; i++){
    var currentChildren = commentList[i].children;
    if(currentChildren && currentChildren.length > 0){
        for(j=0; j<currentChildren.length; j++){
            if(currentChildren[j].id === idToSearch){
                result=currentChildren[j];
                j=currentChildren.length;
                i=commentList.length;
            }
        }
    }
 }
发布评论

评论列表(0)

  1. 暂无评论