简体   繁体   English

获取包含嵌套数组中的值的对象的索引-JavaScript

[英]Get index of object that contains value in a nested array - JavaScript

I've been searching for something to help me on this particular problem but I've not found a suitable solution which works for my case. 我一直在寻找可以帮助我解决此特定问题的方法,但是我没有找到适合我情况的合适解决方案。 I'd really appreciate it if somebody could point me in the right direction. 如果有人可以指出正确的方向,我将非常感激。

I have an array: 我有一个数组:

var ruizTreeData = [


   {
      "Name": "Ruiz Hernandez",
      "parent": "null",
      "nodetype": "person",
      "Country": "assets/img/aml-flag-1.png",
      "children": [
        {
          "Name": "Checking",
          "nodetype": "account",
          "Country": "assets/img/aml-flag-1.png",
          "children": [
            {
              "Name": "Renato Godoy",
              "nodetype": "person",
              "Country": "assets/img/aml-flag-4.png"
            },
            {
              "Name": "Juan Nieto",
              "nodetype": "person",
              "Country": "assets/img/aml-flag-4.png"
            },
            {
              "Name": "Bani Cortes",
              "nodetype": "person",
              "Country": "assets/img/aml-flag-2.png"
            },
            {
              "Name": "Medina Marquez",
              "nodetype": "person",
              "Country": "assets/img/aml-flag-2.png"
            }
          ]
        }
      ]
    }
  ];

I have another function which is creating an object, and what I'd like to do is insert that object into this "tree" at a particular index. 我还有另一个创建对象的函数,我想做的就是将该对象插入到特定索引处的“树”中。 So for example, the user would choose to insert a new person underneath "Ruiz Hernandez", so I'd create an object which would be a sibling of "Checking". 因此,例如,用户将选择在“ Ruiz Hernandez”下方插入一个新用户,因此我将创建一个对象,该对象将是“ Checking”的同级对象。 But I want it to be flexible enough so that if somebody added as a child of "Checking" we'd do the same, or a child of "Bani Cortes" we'd do the same etc. 但我希望它具有足够的灵活性,以便如果有人添加作为“检查”的子代,我们将做同样的事情,或者作为“ Bani Cortes”的子代,我们也将做同样的事情。

I'm thinking I'd have to iterate ruizTreeData until I found "Ruiz Hernandez" and then insert the object into the children array of "Ruiz Hernandez". 我想我必须迭代ruizTreeData直到找到“ Ruiz Hernandez”,然后将对象插入“ Ruiz Hernandez”的children ruizTreeData组中。

So the array would then look like: 因此,该数组将如下所示:

var ruizTreeData = [


   {
      "Name": "Ruiz Hernandez",
      "parent": "null",
      "nodetype": "person",
      "Country": "assets/img/aml-flag-1.png",
      "children": [
        {
          "Name": "Checking",
          "nodetype": "account",
          "Country": "assets/img/aml-flag-1.png",
          "children": [
            {
              "Name": "Renato Godoy",
              "nodetype": "person",
              "Country": "assets/img/aml-flag-4.png"
            }...
          ]
        },
        {
          "Name": "Inserted object",
          "nodetype": "account",
          "Country": "assets/img/aml-flag-1.png"
        }
      ]
    }
  ];

I've tried a bunch of things, the closest I've got is: 我尝试了很多事情,最接近的是:

positionInTree = ruizTreeData
                          .map(function (element) {return element["Name"];})
                          .indexOf("Ruiz Hernandez");

But this only returns at the first index, it doesn't go into the nested objects. 但这仅在第一个索引处返回,而不会进入嵌套对象。 Would I need a for loop for this? 我需要一个for循环吗?

I'd really appreciate some help with this. 非常感谢您的帮助。 Thanks very much. 非常感谢。

There you go. 妳去 You can select your element by a key/value identifier and this function will create a children property if there isn't one already with your object inside it or add your object into the already existing children property. 您可以通过键/值标识符选择元素,如果该对象中没有对象,则此函数将创建子属性,或者将对象添加到现有的子属性中。

I added a person named Ruiz Hernandez into the last node (Checking's children) to show you how the function add your object in every node with your key/value identifier. 我在最后一个节点(Checking的孩子)中添加了一个名为Ruiz Hernandez的人,向您展示了该函数如何使用键/值标识符在每个节点中添加对象。

 var ruizTreeData = [ { "Name": "Ruiz Hernandez", "parent": "null", "nodetype": "person", "Country": "assets/img/aml-flag-1.png", "children": [ { "Name": "Checking", "nodetype": "account", "Country": "assets/img/aml-flag-1.png", "children": [ { "Name": "Renato Godoy", "nodetype": "person", "Country": "assets/img/aml-flag-4.png" }, { "Name": "Juan Nieto", "nodetype": "person", "Country": "assets/img/aml-flag-4.png" }, { "Name": "Bani Cortes", "nodetype": "person", "Country": "assets/img/aml-flag-2.png" }, { "Name": "Ruiz Hernandez", "nodetype": "person", "Country": "assets/img/aml-flag-2.png" }, { "Name": "Medina Marquez", "nodetype": "person", "Country": "assets/img/aml-flag-2.png" } ] } ] } ]; Array.prototype.addChild = function(key, value, object){ let el; if(this.find(x => x[key] == value)){ this.filter(x => x[key] == value).forEach(y => y.children = (y.children) ? [...y.children, {...object}] : [{...object}]) } this.forEach(y => { if(y.children){ y.children.addChild(key, value, object); } }); } ruizTreeData.addChild("Name", "Ruiz Hernandez", {Name: "Name"}); console.log(ruizTreeData); 

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

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