【问题标题】:Get the index of the array when array object matching condition数组对象匹配条件时获取数组的索引
【发布时间】:2022-01-06 10:58:25
【问题描述】:

我有一个这样的数组:

parent = [
    {'id':'id_1','level':'1-1','children':['id':'id_1','level':'1-1-0','children':[........]},
    {'id':'id_2','level':'2-2','children':['id':'id_1','level':'2-1-0','children':[........]}
]

如何获取符合子数组对象条件的父数组索引

例如:

If (level == '2-1-0') 

输出:1

预期行为:

它应该返回 1,因为级别 2-1-0 存在于父数组的第一个索引中

注意: 我试过如下

var index = parent .findIndex(data => data.level== result.level);

它不会检查子数组对象

【问题讨论】:

  • parent 变量中存储的数组在语法上无效:您能更新您的问题吗?另外,children 是否有任意级别的嵌套?
  • 你是对的,这不会检查对象中的孩子。您必须检查每个子数组并对照您的result.level 进行检查。
  • 您的 children 数组不是 va;id 数组。也只是从数组中的子对象中找到匹配级别吗?专门问这个是因为您的子数组已经有另一个子节点。

标签: javascript angular


【解决方案1】:

您提供的数组是错误的,但我认为您需要这样的东西:

const parent = [
    {'id':'id_1','level':'1-1','children':[{'id':'id_1','level':'1-1-0'}]},
    {'id':'id_2','level':'2-2','children':[{'id':'id_1','level':'2-1-0'}]}
]

const index = parent.findIndex(p => p.children.find(c => c.level === '2-1-0') !== undefined);

console.log(index);

在 '2-1-0' 的位置,您应该放置要检查的索引。

【讨论】:

  • 此解决方案不适用于不同的深度(例如,level = '1-3-4-5')
  • 好吧,问题不说深度,但是如果将逻辑封装到一个函数中,我们可以使用递归调用来做到这一点。
【解决方案2】:

这是一个递归问题,对于大数组可能会爆栈。

脚本首先遍历树的深度,并且可以进行优化以检测请求的级别深度并仅检查该级别,从而提高性能。

var weirdProblem = level => (carry, current, idx) => {
  // bail if already found 
  if (carry > -1) {
    return carry;
  }

  // check current item's children and return current idx if matching
  if (current.children.some(child => child.level === level)) {
    return idx;
  }

  // traverse current item's children
  return current.children.reduce(weirdProblem(level), -1);
};

var index = parent.reduce(weirdProblem('2-1-0'), -1);

没有测试脚本。

【讨论】:

    【解决方案3】:

    你需要递归

    const findItemNested = (arr, level, nestingKey) => (
      arr.reduce((a, item, index) => {
        if (a) return a;
        if (item.level === level) return index;
        if (item[nestingKey]) return findItemNested(item[nestingKey], level, nestingKey)
      }, null)
    );
    const testArr = [
          {
            'id':'id_1',
           'level':'1-1',
           'children':[
             {'id':'id_1'},
             {'level':'1-1-0'},
             {'children':[]}
           ]
          },
          {'id':'id_2','level':'2-2','children':[{'id':'id_1'},{'level':'2-1-0'},{'children':[]}]}
      ];
    
    const res = findItemNested(testArr, '2-1-0', "children");
    
    console.log(res) //  1
    

    【讨论】:

    • 它满足了我 75% 的需求,我在这段代码中做了一些更新。
    【解决方案4】:

    你的父数组格式错误,我猜应该是嵌套结构。

    由于孩子也可能包含更多孩子,因此您需要递归进行深度搜索。

    const parent = [
        {"id": "id_1", "level": "1-1", "children": [{"id": "id_1", "level": "1-1-0", "children": []}]},
        {"id": "id_2", "level": "2-2", "children": [{"id": "id_1", "level": "2-1-0", "children": []}]}
    ];
    
    function levelIndex(children, level) {
        return children.findIndex(child => child.level == level || levelIndex(child.children, level) >= 0);
    }
        
    const index = levelIndex(parent, "2-1-0");
    console.log("Found index:", index)

    【讨论】:

      【解决方案5】:

      在 .TS 文件中:-

      const parent = [
          {'id':'id_1',
           'level':'1-1',
           'children':[{'id':'id_1',
                        'level':'1-1-0'}]},
                       {'id':'id_2',
                        'level':'2-2',
                        'children':[{'id':'id_1',
                                     'level':'2-1-0'}]}
      ]
      
      const index = parent.filter(x => x.children.find(x => x.level === '2-1-0') !== undefined);
      
      console.log(index);
      

      试试这个。我认为它会起作用。

      【讨论】:

        猜你喜欢
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-27
        • 1970-01-01
        • 2012-02-21
        • 2014-05-14
        相关资源
        最近更新 更多