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". 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.
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".
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?
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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.