JavaScript Arrange JSON in tree structure

I have a plain JSON that contasins the id's of its dependentant's objects..

 var array1=  

    [
        {
            "id": 84,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": null,
            "isRemoved": false,
            "isPrimary": false
        },
        {
            "id": 95,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": 84,
            "isRemoved": false,
            "isPrimary": false
        },
        {
            "id": 150,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": 95,
            "isRemoved": false,
            "isPrimary": false
        },
        {
            "id": 160,
            "outputTypeId": 900000000000002,
            "previousOutputTypeActivitySeqMappingId": 95,
            "isRemoved": false,
            "isPrimary": false
        }
    ]

I wanted to convert to the below format by identifying the "id" and "previousOutputTypeActivitySeqMappingId" and pushing it into a new array called items

var array1=

    [{
        "id": 84,
        "outputTypeId": 900000000000002,
        "previousOutputTypeActivitySeqMappingId": null,
        "isRemoved": false,
        "isPrimary": false,
        "items": [
            {
                "id": 95,
                "outputTypeId": 900000000000002,
                "previousOutputTypeActivitySeqMappingId": 84,
                "isRemoved": false,
                "isPrimary": false,
                "items": [
                    {
                        "id": 150,
                        "outputTypeId": 900000000000002,
                        "previousOutputTypeActivitySeqMappingId": 95,
                        "isRemoved": false,
                        "isPrimary": false,
                        "items": []
                    },
                    {
                        "id": 160,
                        "outputTypeId": 900000000000002,
                        "previousOutputTypeActivitySeqMappingId": 95,
                        "isRemoved": false,
                        "isPrimary": false,
                        "items": []
                    }
                ]
            }
        ]
    }]

The code that I tried is given below... I created a dummy object first in the new format:

var dummyObj= {
        "id": 84,
        "outputTypeId": 900000000000002,
        "previousOutputTypeActivitySeqMappingId": null,
        "isRemoved": false,
        "isPrimary": false
    }
function populateObj(array1, arrObj) {
   for (var i = 0; i < array1.length; i++) {
        if (array1[i].id == arrObj.parentActivityId) {
            array1[i].items.push(arrObj);
        } else {
            populateObj(array1[i].items, arrObj);
        }
    }
};

populateObj(dummyObj);

Since I don't want to hardcode a dummy object in the beginning. Is there any way to achieve this conversion using javascript?
Thanks.

Answer:1
function group(arr) {
    var t={};
    arr.forEach(function(obj) {
        if (!obj.id) throw 'object found without id!';
        t[obj.id]=obj;
    });
    var result=[];
    arr.forEach(function(obj) {
        if (!obj.previousOutputTypeActivitySeqMappingId) {
            result.push(obj);
        } else {
            var parent=t[obj.previousOutputTypeActivitySeqMappingId];
            if (!parent) throw('parent with id '+obj.previousOutputTypeActivitySeqMappingId+' not found!');
            var items=parent.items;
            if (!items) {
                items=[];
                parent.items=items;
            }
            items.push(obj);
        }
    });
    return result;
}
Answer:2

I have a div like this : <div id="div_more_info"> <input placeholder="title" name="title_info" class="title_info" style="margin: 1px" type="text"> <input placeholder="des" name=...

I have a div like this : <div id="div_more_info"> <input placeholder="title" name="title_info" class="title_info" style="margin: 1px" type="text"> <input placeholder="des" name=...

  1. next element academy
  2. next element sibling
  3. next element jquery
  4. next element in list python
  5. next element javascript
  6. next element css
  7. next element homes
  8. next element child
  9. next element bjj
  10. next element academy schedule
  11. next element bulldogs
  12. next element fencing
  13. next element homes jacksonville fl
  14. next element xpath
  15. next element in vector c++
  16. next element boy band
  17. next element beautifulsoup
  18. next element to be discovered
  19. next elementary school strike
  20. next elementary strike

for some reason when I run npm start and I hit the browser, I am getting this stack trace with this error. TypeError: $.ajax is not a function at getLocationFromIp (G:\Github\Expressjs\nodetest1\...

for some reason when I run npm start and I hit the browser, I am getting this stack trace with this error. TypeError: $.ajax is not a function at getLocationFromIp (G:\Github\Expressjs\nodetest1\...

I am no beginner in javascript. I am actually working on this for past 3-4 months but today I read this statement about "What is JavaScript?" JavaScript is single-threaded, non-blocking, ...

I am no beginner in javascript. I am actually working on this for past 3-4 months but today I read this statement about "What is JavaScript?" JavaScript is single-threaded, non-blocking, ...

  1. does javascript work on iphone
  2. does javascript work on mobile
  3. does javascript work on android
  4. does javascript work on all browsers
  5. does javascript work in email
  6. does javascript work on ipad
  7. does javascript work in html
  8. does javascript work offline
  9. does javascript work on notepad
  10. does javascript work on mac
  11. does javascript work in chrome
  12. does javascript work locally
  13. does javascript work in html email
  14. does javascript work in wordpress
  15. does javascript work in typescript
  16. does javascript work on safari
  17. does javascript work with php
  18. does javascript work in outlook
  19. does javascript work on apple
  20. does javascript work without internet

My Parse Javascript cloudcode is not working correctly. Basically all my objects have a corresponding "index/menindex & womenindex" number. If both genders are selected- all items show up (as ...

My Parse Javascript cloudcode is not working correctly. Basically all my objects have a corresponding "index/menindex & womenindex" number. If both genders are selected- all items show up (as ...