I've been trying to figure out a way of doing this. This is my object:
{
"id":1,
"name":"Blooper Corp.",
"emoji":"",
"parent_id":null,
"children":[
{
"id":2,
"name":"Food",
"emoji":"",
"parent_id":1,
"children":[
]
},
{
"id":3,
"name":"Canine Therapy",
"emoji":"",
"parent_id":1,
"children":[
{
"id":4,
"name":"Massages",
"emoji":"",
"parent_id":3,
"children":[
]
},
{
"id":5,
"name":"Games",
"emoji":"",
"parent_id":3,
"children":[
]
}
]
}
]
}
I'm trying to get the last id
of the array so I can use it in the new child array and add it as a child to that specific tier, for example:
{
"id":6, // New id
"name":"Music",
"emoji":"",
"parent_id":4, //this will be the parent id it belongs to
"children":[
]
}
This is my javascript button function:
function askUserForTeamDetails( team ) {
const emoji = prompt( 'Enter new team’s emoji:' );
if ( null === emoji ) {
return;
}
const name = prompt( 'Enter new team’s name:' );
if ( null === name ) {
return;
}
let tree = getTree();
tree.id = {}; //new child array to push depending on parent id
return { name, emoji };
}
getTree()
:
const tree = {"id":1,"name":"Rusty Corp.","emoji":"","parent_id":null,"children":[{"id":2,"name":"Food","emoji":"","parent_id":1,"children":[]},{"id":3,"name":"Canine Therapy","emoji":"","parent_id":1,"children":[{"id":4,"name":"Massages","emoji":"","parent_id":3,"children":[]},{"id":5,"name":"Games","emoji":"","parent_id":3,"children":[]}]}]};
return tree;
I've tried using Object.keys(tree)[Object.keys(tree).length-1];
But I don't see how this will work since it's multidimensional.
I hope someone can advise a way of doing this.
Thanks