we have an array like this. I want to add the new child and increment the "id".
[
{
"name":"Headcount",
"id":1,
"parentId":0,
"is_open":true,
"children":[
{
"name":"temp1",
"id":2,
"parentId":1,
"is_open":true,
},
{
"name":"Temp",
"id":90
}
]
},
{
"name":"temp2",
"id":4,
"parentId":0,
"is_open":true,
"children":[
{
"name":"temp3",
"id":5,
"parentId":4,
"is_open":true,
"children":[
{
"name":"temp4",
"id":6,
"parentId":5,
"is_open":true }
]
}
]
We want to add a new ID to the newly added child node.
we have tried adding the new id by finding the max value in an array and add it to the new child node.
the code we tried is
var res = Math.max.apply(Math,data.map(function(o){return o.id;}))
console.log("Max ID res:"+res);
It gives the answer as "4" but we want it "90" since it is the largest number. how to iterate through Child nodes and find/increment the new "id".
Thanks in advance for your help.