1

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

designtocode
  • 2,215
  • 4
  • 21
  • 35

3 Answers3

2

If you want to add new object based on parent_id property that can be on any level, you can create recursive function that will traverse the object with for...in loop and push the object to children if the id matches.

const data = {"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":[]}]}]}

function insert(data, obj, pid = null) {
  if (pid == obj.parent_id) {
    data.children.push(obj);
    return;
  }

  if (data.children) {
    data.children.forEach(e => insert(e, obj, e.id))
  }
}

function getLastId(data) {
  let result = null;

  function search(data) {
    if (!result || data.id > result) result = data.id;
    if (data.children)  data.children.forEach(search);
  }

  search(data);
  return result;
}

const newObj = {
  "id": getLastId(data) + 1,
  "name": "Music",
  "emoji": "",
  "parent_id": 4,
  "children": []
}

insert(data, newObj);
console.log(data)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

You could write a recursive function that would loop over each of the children until the id was found. One way of doing this could be:

function findTeamById(id, tree) {
  if (id === tree.id) return tree //returning tree as it is the current team's object
  for (let i = 0; i < tree.length; i++) {
    let teamTree = findTeamById(id, tree[i])
    if (teamTree !== false) {
      // return the found tree
      return teamTree
    }
  }
  // if no tree was found return false
  return false
}
// EDIT: forgot to show how to add the child
const newTeam = {
  "id":6, // New id
  "name":"Music",
  "emoji":"",
  "parent_id":4, //this will be the parent id it belongs to
  "children":[

  ]
}

// this will push the new team to the childrens array of the newTeams parent
findTeamById(newTeam.parent_id, myTree).children.push(newTeam)
0

It seems like you'll need a recursive function to find the max ID, then get the parent of that maxID object and put the new object in its children.

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":[]}]}]};


const newObj = {
  "id": 6,
  "name": "Music",
  "emoji": "",
  "parent_id": 0,
  "children": []
}

var maxID=0;
var parentObj = null; 
function findMaxIDArray(obj) {
  var tmpid = maxID;
  maxID = Math.max(obj.id, maxID);
  if (maxID>tmpid) {
      parentObj = obj;
  }
  if (obj.children) {
    for(let c=0;c<obj.children.length;c++){
      if (obj.children[c]) {
         findMaxIDArray(obj.children[c]);
      }
    }
  }  
}



findMaxIDArray(tree);

if (parentObj) {
  newObj.parent_id=parentObj.id;
  parentObj.children.push(newObj);
}

console.log(tree);
nixkuroi
  • 2,259
  • 1
  • 19
  • 25