-1

I have an array of objects like this [Object, Object, Object]

the structure of every object like this

0: Object
   children: Array[0]
   node_id: 0
   parent_id: -2
   text: "(r , e , w)"
   value: 6

1: Object
   children: Array[0]
   node_id: 1
   parent_id: 0
   text: "(r)"
   value: 3

2: Object
   children: Array[0]
   node_id: 2
   parent_id: 0
   text: "(e , w)"
   value: 3

first I have for loop and I want to get parent_id every iteration

second I want to put last two objects into the children array inside first object Any help appreciated.

brothers28
  • 1,196
  • 17
  • 23
Mohamed Hana
  • 378
  • 4
  • 10
  • Can you please be more clear about what you want to have? Can you also tell us what you have done so far? – brothers28 Feb 02 '15 at 13:19
  • I want to build a tree structure I want to put the last two objects into the first object children array – Mohamed Hana Feb 02 '15 at 13:21
  • Then have a look at this post here http://stackoverflow.com/questions/18017869/build-tree-array-from-flat-array-in-javascript – brothers28 Feb 02 '15 at 13:25

1 Answers1

0
arrayOfObjects.forEach(function(obj) {
    console.log(obj.parent_id);
    arrayOfObjects[0].children.push(obj.text);
    arrayOfObjects[0].children.push(obj.value);
});
Johan Sundén
  • 447
  • 4
  • 9