最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Not able to push an object into parent array by identifying the parent id of the object in javascript - Stack Overflow

programmeradmin6浏览0评论

I'm having a JSON array with properties as id and parentActivityId.

$scope.data = [
    {
        id: 1,
        activityName: "Drilling",
        parentActivityId: 0,
        items: [
            {
                id: 2,
                activityName: "Blasting",
                parentActivityId: 1,
                items: [
                    {
                        id: 3,
                        activityName: "Ann",
                        parentActivityId: 2,
                        items: [],

                    },
                    {
                        id: 4,
                        activityName: "Ann",
                        parentActivityId: 2,
                        items: [],

                    }
                ]
            },
            {
                id: 5,
                activityName: "Transport",
                parentActivityId: 1,
                items: [
                    {
                        id: 6,
                        activityName: "Daniel",
                        parentActivityId: 5,
                        items: [],
                    }
                ]
            }
        ]
    }
];

I would like to push a new item based on the parentActiityId.The new item will be having a new id. Example:If my object is like this:

{
    id: 7,
    activityName: "Drilling",
    parentActivityId: 1,
    items: []
}

then my object will look like this..

 $scope.data = [
        {
            id: 1,
            activityName: "Drilling",
            parentActivityId: 0,
            items: [
                {
                    id: 2,
                    activityName: "Blasting",
                    parentActivityId: 1,
                    items: [
                        {
                            id: 3,
                            activityName: "Ann",
                            parentActivityId: 2,
                            items: [],

                        },
                        {
                            id: 4,
                            activityName: "Ann",
                            parentActivityId: 2,
                            items: [],

                        },
                    {
                         id: 7,
                         activityName: "Drilling",
                         parentActivityId: 2,
                         items: [],
                     }
                    ]
                },
                {
                    id: 5,
                    activityName: "Transport",
                    parentActivityId: 1,
                    items: [
                        {
                            id: 6,
                            activityName: "Daniel",
                            parentActivityId: 5,
                            items: [],
                        }
                    ]
                }
            ]
        }
    ];

I tried by giving this for loop..

var arrObj = {
    id: 7,
    activityName: "Drilling",
    parentActivityId: 1,
    items: []
};

function populateObj(arrObj) {
   for (var i = 0; i < $scope.data.length; i++) {
        if ($scope.data[i].id == arrObj.parentActivityId) {
            $scope.data.push(arrObj);
        }
    }
};

populateObj(arrObj);

which will push only to the parent.I want to identify the child as well in the for loop and the push to the specific array object by identifying thr parentActivityId.Any help will be really appreciated.

I'm having a JSON array with properties as id and parentActivityId.

$scope.data = [
    {
        id: 1,
        activityName: "Drilling",
        parentActivityId: 0,
        items: [
            {
                id: 2,
                activityName: "Blasting",
                parentActivityId: 1,
                items: [
                    {
                        id: 3,
                        activityName: "Ann",
                        parentActivityId: 2,
                        items: [],

                    },
                    {
                        id: 4,
                        activityName: "Ann",
                        parentActivityId: 2,
                        items: [],

                    }
                ]
            },
            {
                id: 5,
                activityName: "Transport",
                parentActivityId: 1,
                items: [
                    {
                        id: 6,
                        activityName: "Daniel",
                        parentActivityId: 5,
                        items: [],
                    }
                ]
            }
        ]
    }
];

I would like to push a new item based on the parentActiityId.The new item will be having a new id. Example:If my object is like this:

{
    id: 7,
    activityName: "Drilling",
    parentActivityId: 1,
    items: []
}

then my object will look like this..

 $scope.data = [
        {
            id: 1,
            activityName: "Drilling",
            parentActivityId: 0,
            items: [
                {
                    id: 2,
                    activityName: "Blasting",
                    parentActivityId: 1,
                    items: [
                        {
                            id: 3,
                            activityName: "Ann",
                            parentActivityId: 2,
                            items: [],

                        },
                        {
                            id: 4,
                            activityName: "Ann",
                            parentActivityId: 2,
                            items: [],

                        },
                    {
                         id: 7,
                         activityName: "Drilling",
                         parentActivityId: 2,
                         items: [],
                     }
                    ]
                },
                {
                    id: 5,
                    activityName: "Transport",
                    parentActivityId: 1,
                    items: [
                        {
                            id: 6,
                            activityName: "Daniel",
                            parentActivityId: 5,
                            items: [],
                        }
                    ]
                }
            ]
        }
    ];

I tried by giving this for loop..

var arrObj = {
    id: 7,
    activityName: "Drilling",
    parentActivityId: 1,
    items: []
};

function populateObj(arrObj) {
   for (var i = 0; i < $scope.data.length; i++) {
        if ($scope.data[i].id == arrObj.parentActivityId) {
            $scope.data.push(arrObj);
        }
    }
};

populateObj(arrObj);

which will push only to the parent.I want to identify the child as well in the for loop and the push to the specific array object by identifying thr parentActivityId.Any help will be really appreciated.

Share Improve this question edited Aug 19, 2015 at 10:32 forgottofly asked Aug 19, 2015 at 9:49 forgottoflyforgottofly 2,72912 gold badges54 silver badges97 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

With a recursive proposal and Array.prototype.reduce():

var data=[{id:1,activityName:"Drilling",parentActivityId:0,items:[{id:2,activityName:"Blasting",parentActivityId:1,items:[{id:3,activityName:"Ann",parentActivityId:2,items:[]},{id:4,activityName:"Ann",parentActivityId:2,items:[]}]},{id:5,activityName:"Transport",parentActivityId:1,items:[{id:6,activityName:"Daniel",parentActivityId:5,items:[]}]}]}],
    child = { id: 7, activityName: "Drilling", parentActivityId: 1, items: [] };

function getParent(r, a) {
    return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r);
}

var node = data.reduce(getParent, {});
'items' in node && node.items.push(child);

document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

You were close to what you tried to achieve. This should do the trick:

var arrObj = {
    id: 7,
    activityName: "Drilling",
    parentActivityId: 1,
    items: []
};

function populateObj(data, arrObj) {
   for (var i = 0; i < data.length; i++) {
        if (data[i].id == arrObj.parentActivityId) {
            data[i].items.push(arrObj);
        } else {
            populateObj(data[i].items, arrObj);
        }
    }
};

populateObj(arrObj);

To check the objects inside items array as well,you can do something like this,

var objectList = [
        {
            id: 1,
            activityName: "Drilling",
            parentActivityId: 0,
            items: [
                {
                    id: 2,
                    activityName: "Blasting",
                    parentActivityId: 1,
                    items: [
                        {
                            id: 3,
                            activityName: "Ann",
                            parentActivityId: 2,
                            items: [],

                        },
                        {
                            id: 4,
                            activityName: "Ann",
                            parentActivityId: 2,
                            items: [],

                        },
                    {
                         id: 7,
                         activityName: "Drilling",
                         parentActivityId: 2,
                         items: [],
                     }
                    ]
                },
                {
                    id: 5,
                    activityName: "Transport",
                    parentActivityId: 1,
                    items: [
                        {
                            id: 6,
                            activityName: "Daniel",
                            parentActivityId: 5,
                            items: [],
                        }
                    ]
                }
            ]
        }
    ];

var arrObj = {
    id: 7,
    activityName: "Drilling",
    parentActivityId: 1,
    items: []
};

function populateObj(ItemList) {
   for (var i = 0; i < ItemList.length; i++) {
        if (ItemList[i].id == arrObj.parentActivityId) {
            ItemList[i].items.push(arrObj);
        }
       else
       {
         populateObj(ItemList[i].items);
       }
    }
};

populateObj(objectList);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论