简体   繁体   English

在 Javascript 中的嵌套 Object 中查找分支

[英]Find branches in nested Object in Javascript

I have tried to find a solution for my problem in the last two hours, which included trying myself, scanning lodash docs as well as SO for any suitable answer, but did not come up with anything remotely working or practical.在过去的两个小时内,我试图为我的问题找到解决方案,其中包括尝试自己、扫描 lodash 文档以及 SO 以获得任何合适的答案,但没有提出任何远程工作或实用的方法。 I would be very grateful for help.我将非常感谢您的帮助。

I have an object that can be any depth.我有一个可以是任意深度的 object。

eg例如

{
  "name": "alpha",
  "children": [
    {
      "name": "beta",
      "children": [
        {
          "name": "gamma",
          "children": [
            {
              "name": "delta",
              "children": []
            }
          ]
        }
      ]
    },
    {
      "name": "epsilon",
      "children": [
        {
          "name": "zeta",
          "children": [
            {
              "name": "eta",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

I am looking for a function which will return the whole branch of this object where there is a matching name (if possible without lodash but if really needed its ok).我正在寻找一个 function 它将返回这个 object 的整个分支,其中有一个匹配的名称(如果可能没有 lodash,但如果真的需要它就可以了)。

Given an input of 'gamma' I expect it to return给定'gamma'的输入,我希望它返回


{
  "name": "alpha",
  "children": [
    {
      "name": "beta",
      "children": [
        {
          "name": "gamma",
          "children": [
            {
              "name": "delta",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

Given an input 't' I expect it to return the whole object, since it is included in the names of children in both branches.给定输入't'我希望它返回整个 object,因为它包含在两个分支的孩子的名字中。

You can separate this problem into two parts, first let's test if the name is present in the tree:您可以将此问题分为两部分,首先让我们测试名称是否存在于树中:

function hasStr(item, str) {
  return item.name.includes(str) || item.children.some(x => hasStr(x, str));
}

hasStr(item, 'gamma'); // true

You also asked to have a function that returns the passed object and return only a filtered version of the children:您还要求有一个 function 返回传递的 object 并仅返回子级的过滤版本:

function possibleAnswer(item, str) {
  return {
    name: item.name,
    children: item.children.filter(x => hasStr(x, str)),
  };
}

possibleAnswer(item, 'gamma'); // will return desired object

This problem can be solved with recursion as the depth is not know.这个问题可以通过递归来解决,因为深度是未知的。

getNames = (nameChild, match) => {
     if (nameChild.length < 1) return false;

     if (nameChild.name.includes(match)) return true;

     k =  nameChild.children
                   .filter((child) => 
                         getNames(child, match)) 
     return (k.length > 0) ? k : false; 
}

suppose the object is assign to nameObj variable then假设 object 被分配给nameObj变量然后

nameObj.children = getNames(nameObj, 'zeta');

this will do the work!这将完成工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM