如何实现"getAllChildrenById"方法 [英] How to implement the 'getAllChildrenById' method

查看:85
本文介绍了如何实现"getAllChildrenById"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样一个数据数组.如何使用lodash方法_.filter通过parentTd(数组"parentIds"之一)实现过滤?

There is such an array of data. How do I implement filtering by parentTd (one of the array "parentIds"), using lodash method _.filter?

"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
    }]

推荐答案

您可以将filterincludes结合使用,以过滤出parentIds包含特定ID的条目.

You can use filter in combination with includes to filter out the entries where parentIds contains a certain id.

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

此外,您不需要lodash:

Also, you do not need lodash:

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

这篇关于如何实现"getAllChildrenById"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆