1 回答
TA贡献1859条经验 获得超6个赞
function parse(data) {
const hash = {};
data.slice().forEach(item => {
const path = item.path;
if (!hash[path]) {
hash[path] = item;
} else {
Object.keys(item).forEach(key => {
const hashPath = hash[path];
if (key !== "path") {
const val = item[key];
if (!hashPath[key]) {
hashPath[key] = val;
}
else {
if (!Array.isArray(hashPath[key])) {
hashPath[key] = [hashPath[key]];
}
hashPath[key] = hashPath[key].concat(val);
}
}
})
}
});
return Object.keys(hash).map(key => hash[key]);
}
var test = [
{
"path": "client_01",
"client_list": [
{
"client": "test_01",
}
],
"share_type": 1,
},
{
"path": "client_01",
"group_list": [
{
"group": "groupData",
}
],
"user_list": [
{
"user": "userData",
}
],
"share_type": 2,
}, {
"path": "client_02",
"client_list": [
{
"client": "test_02",
}
],
"share_type": 1,
},
{
"path": "client_02",
"group_list": [
{
"group": "groupData_02",
}
],
"user_list": [
{
"user": "userData_02",
}
],
"share_type": 2,
}
];
console.log(parse(test));
添加回答
举报