关于数组比较问题,javascript
vararry=[{"LOGINID":"dep","NAME":"公司高管","ORGID":3,"ORGPID":1},{"LOGINID":"wws","NAME"...
var arry = [
{"LOGINID": "dep", "NAME": "公司高管", "ORGID": 3, "ORGPID": 1},
{"LOGINID": "wws", "NAME": "吴万善", "ORGID": 1810000, "ORGPID": 3},
{"LOGINID": "ywh", "NAME": "俞文宏", "ORGID": 1850000, "ORGPID": 3},
{"LOGINID": "dep", "NAME": "权益投资部", "ORGID": 4, "ORGPID": 1},
{"LOGINID": "yingshuai", "NAME": "应帅", "ORGID": 2340000, "ORGPID": 4},
{"LOGINID": "caiwangpeng", "NAME": "蔡望鹏", "ORGID": 2650000, "ORGPID": 4}
];
最后结果形式我想这样的
[ {"NAME":"公司高管","ORGID":3,"LOGINID":[{"NAME":"吴万善","ORGPID":3},{"NAME":"俞文宏","ORGPID":3}]},{"NAME":"权益投资部","ORGID":4,"LOGINID":[{"NAME":"应帅","ORGPID":4},{"NAME":"蔡望鹏","ORGPID":4}]}, . . . ]
求解决,谢谢 展开
{"LOGINID": "dep", "NAME": "公司高管", "ORGID": 3, "ORGPID": 1},
{"LOGINID": "wws", "NAME": "吴万善", "ORGID": 1810000, "ORGPID": 3},
{"LOGINID": "ywh", "NAME": "俞文宏", "ORGID": 1850000, "ORGPID": 3},
{"LOGINID": "dep", "NAME": "权益投资部", "ORGID": 4, "ORGPID": 1},
{"LOGINID": "yingshuai", "NAME": "应帅", "ORGID": 2340000, "ORGPID": 4},
{"LOGINID": "caiwangpeng", "NAME": "蔡望鹏", "ORGID": 2650000, "ORGPID": 4}
];
最后结果形式我想这样的
[ {"NAME":"公司高管","ORGID":3,"LOGINID":[{"NAME":"吴万善","ORGPID":3},{"NAME":"俞文宏","ORGPID":3}]},{"NAME":"权益投资部","ORGID":4,"LOGINID":[{"NAME":"应帅","ORGPID":4},{"NAME":"蔡望鹏","ORGPID":4}]}, . . . ]
求解决,谢谢 展开
1个回答
展开全部
var arry = [
{"LOGINID": "dep", "NAME": "公司高管", "ORGID": 3, "ORGPID": 1},
{"LOGINID": "wws", "NAME": "吴万善", "ORGID": 1810000, "ORGPID": 3},
{"LOGINID": "ywh", "NAME": "俞文宏", "ORGID": 1850000, "ORGPID": 3},
{"LOGINID": "dep", "NAME": "权益投资部", "ORGID": 4, "ORGPID": 1},
{"LOGINID": "yingshuai", "NAME": "应帅", "ORGID": 2340000, "ORGPID": 4},
{"LOGINID": "caiwangpeng", "NAME": "蔡望鹏", "ORGID": 2650000, "ORGPID": 4}
];
var convert = function(arr){
var result = [], map = {};
for(var i=0;i<arr.length;i++){
var ai = arr[i];
if(ai.ORGPID == 1){
map[ai.ORGID] = [];
result.push({
"NAME": ai.NAME,
"ORGID": ai.ORGID,
"LOGINID": map[ai.ORGID]
});
}else if(!!map[ai.ORGPID]){
map[ai.ORGPID].push({
"NAME": ai.NAME,
"ORGPID": ai.ORGPID
});
}
}
return result;
}
var arr = convert (arry);
console.log(arr);
追问
大赞,佩服的就是你们这些大牛,我双重for循环搞了一上午都没搞出来,桑不起
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询