本文介绍了无法通过识别JavaScript中的对象的父ID推对象为母公司数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用属性ID和parentActivityId一个JSON阵列。

  $ scope.data = [
    {
        ID:1,
        activityName:钻孔,
        parentActivityId:0,
        项目:
            {
                ID:2,
                activityName:爆破,
                parentActivityId:1,
                项目:
                    {
                        ID:3,
                        activityName:安安,
                        parentActivityId:2,
                        项目:[],                    },
                    {
                        ID:4,
                        activityName:安安,
                        parentActivityId:2,
                        项目:[],                    }
                ]
            },
            {
                ID:5,
                activityName:运输,
                parentActivityId:1,
                项目:
                    {
                        编号:6,
                        activityName:丹尼尔
                        parentActivityId:5,
                        项目:[],
                    }
                ]
            }
        ]
    }
];

我想推根据 parentActiityId 。新的项目将有一个新的 ID 。
例如:如果我的对象是这样的:

  {
    ID:7,
    activityName:钻孔,
    parentActivityId:1,
    项目:[]
}

然后我的对象将是这样的..

  $ scope.data = [
        {
            ID:1,
            activityName:钻孔,
            parentActivityId:0,
            项目:
                {
                    ID:2,
                    activityName:爆破,
                    parentActivityId:1,
                    项目:
                        {
                            ID:3,
                            activityName:安安,
                            parentActivityId:2,
                            项目:[],                        },
                        {
                            ID:4,
                            activityName:安安,
                            parentActivityId:2,
                            项目:[],                        },
                    {
                         ID:7,
                         activityName:钻孔,
                         parentActivityId:2,
                         项目:[],
                     }
                    ]
                },
                {
                    ID:5,
                    activityName:运输,
                    parentActivityId:1,
                    项目:
                        {
                            编号:6,
                            activityName:丹尼尔
                            parentActivityId:5,
                            项目:[],
                        }
                    ]
                }
            ]
        }
    ];

我试图给这个循环。

  VAR arrObj = {
    ID:7,
    activityName:钻孔,
    parentActivityId:1,
    项目:[]
};功能populateObj(arrObj){
   对于(VAR I = 0; I< $ scope.data.length;我++){
        如果($ scope.data [I] .ID == arrObj.parentActivityId){
            $ scope.data.push(arrObj);
        }
    }
};populateObj(arrObj);

这将推动仅对parent.I通过识别THR parentActivityId.Any的帮助将是非常美联社preciated以确定孩子以及在for循环,并推动特定数组对象。


解决方案

通过递归的建议,的:

\r
\r

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:[]}]}]}],\r
    孩子= {ID:7,activityName:钻孔,parentActivityId:1,项目:[]};\r
\r
功能的getParent(R,A){\r
    返回a.id === child.parentActivityId?答:a.items.reduce(的getParent,R);\r
}\r
\r
变种节点= data.reduce(的getParent,{});\r
'项目'的节点放大器;&安培; node.items.push(小孩);\r
\r
的document.write('< pre>'+ JSON.stringify(数据,0,4)+'< / pre>');

\r

\r
\r

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.

解决方案

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>');

这篇关于无法通过识别JavaScript中的对象的父ID推对象为母公司数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:22