Javascript 如何实施';getAllChildrenById';方法

Javascript 如何实施';getAllChildrenById';方法,javascript,lodash,Javascript,Lodash,有这样一组数据。如何使用lodash方法u2; filter实现parentTd(数组“parentid”之一)的过滤 "terms": [{ "id": 13, "name": 'illustrator', "parentIds": [2, 4], "isCompanyArea": false }, { "id": 14, "name": 'figma', "parentIds": [2,

有这样一组数据。如何使用lodash方法u2; filter实现parentTd(数组“parentid”之一)的过滤

"terms": [{
      "id": 13,
      "name": 'illustrator',
      "parentIds": [2, 4],
      "isCompanyArea": false
    },
    {
      "id": 14,
      "name": 'figma',   
      "parentIds": [2, 3],
      "isCompanyArea": true
    },
    {
      "id": 15,
      "name": 'sas',
      "parentIds": [3 ,4, 2],
      "isCompanyArea": false
    },
    {
      "id": 16,
      "name": 'jmp',
      "parentIds": [3],
      "isCompanyArea": false
    },
    {
      "id": 17,
      "name": 'docker',
      "parentIds": [4, 5],
      "isCompanyArea": false
    }]

您可以使用
Array.filter()
Array.includes()

const getAllChildrenById=searchParentId=>
terms.filter(({parentId})=>parentId.includes(searchParentId))
const terms=[{“id”:13,“name”:“illustrator”,“parentId”:[2,4],“isCompanyArea”:false},{“id”:14,“name”:“figma”,“parentId”:[2,3],“isCompanyArea”:true},{“id”:15,“name”:“sas”,“parentId”:[3,4,2],“isCompanyArea”:false},{“id”:16,“name”:“jmp”,“parentId”:“parentId”:“isCompanyArea”:false},{“id”:17,“name”:“docker”,“parentId”:“parentId”:“parentId”:[4,5],”false}
const result=getAllChildrenById(4)

console.log(result)
如果js已经提供了您所需要的一切,为什么要使用lodash

const items = terms.filter(item => item.parentIds && item.parentIds.includes(myParentId));

您可以将
filter
includes
结合使用,以筛选出
parentId
包含特定id的条目

function filter(id) {
    return _.filter(terms, term => term.parentIds.includes(id));
}
此外,您不需要lodash:

function filter(id) {
    return terms.filter(term => term.parentIds.includes(id));
}

预期产量是多少?