我有这样的对象数组。他们有重复的属性contactName
和categoryName
值
[
{
categoryId:1
categoryName:"Default"
contactName:"Anonymous"
name:"Mobile"
value:"+4417087654"
},
{
categoryId:1
categoryName:"Default"
contactName:"Anonymous"
name:"Email"
value:"test2@gmail.com"
},
{
categoryId:1
categoryName:"Default"
contactName:"Test"
name:"Email"
value:"ivdtest@test.com"
},
{
categoryId:1
categoryName:"Default"
contactName:"Test"
name:"Mobile"
value:"+4417087654"
},
{
categoryId:2
categoryName:"Personal"
contactName:"Anonymous"
name:"Email"
value:"ivdtest@test.com"
},
{
categoryId:2
categoryName:"Personal"
contactName:"Anonymous"
name:"Email"
value:"ivdtest@test.com"
},
{
categoryId:2
categoryName:"Personal"
contactName:"Test"
name:"Email"
value:"ivdtest@test.com"
},
{
categoryId:2
categoryName:"Personal"
contactName:"Test"
name:"Email"
value:"ivdtest2@test.com"
}
]
我想按照属性contactName
和categoryName
可能是这样的
[
{
"categoryId": 1,
"categoryName": "Default",
entity: [
{
"contactName": "Anonymous",
"addresses": [
{
"name": "Mobile",
"value": "+4417087654"
},
{
"name": "Email",
"value": "test2@gmail.com"
},
{
"name": "Email",
"value": "ivdtest@test.com"
}
]
},
{
"contactName": "Test",
"addresses": [
{
"name": "Mobile",
"value": "+4417087654"
},
{
"name": "Email",
"value": "test2@gmail.com"
},
{
"name": "Email",
"value": "ivdtest@test.com"
}
]
}]
},
{
"categoryId": 2,
"categoryName": "Personal",
entity: [
{
"contactName": "Anonymous",
"addresses": [
{
"name": "Mobile",
"value": "+4417087654"
},
{
"name": "Email",
"value": "test2@gmail.com"
},
{
"name": "Email",
"value": "ivdtest@test.com"
}
]
},
{
"contactName": "Test",
"addresses": [
{
"name": "Mobile",
"value": "+4417087654"
},
{
"name": "Email",
"value": "test2@gmail.com"
},
{
"name": "Email",
"value": "ivdtest@test.com"
}
]
}]
}
]
答案 0 :(得分:2)
var output = _.groupBy(input, function(entry) {
return entry.categoryName + ',' + entry.contactName;
});
答案 1 :(得分:1)
在普通的Javascript中,您可以为嵌套组使用辅助对象。
var data = [{ categoryId: 1, categoryName: "Default", contactName: "Anonymous", name: "Mobile", value: "+4417087654" }, { categoryId: 1, categoryName: "Default", contactName: "Anonymous", name: "Email", value: "test2@gmail.com" }, { categoryId: 1, categoryName: "Default", contactName: "Test", name: "Email", value: "ivdtest@test.com" }, { categoryId: 1, categoryName: "Default", contactName: "Test", name: "Mobile", value: "+4417087654" }, { categoryId: 2, categoryName: "Personal", contactName: "Anonymous", name: "Email", value: "ivdtest@test.com" }, { categoryId: 2, categoryName: "Personal", contactName: "Anonymous", name: "Email", value: "ivdtest@test.com" }, { categoryId: 2, categoryName: "Personal", contactName: "Test", name: "Email", value: "ivdtest@test.com" }, { categoryId: 2, categoryName: "Personal", contactName: "Test", name: "Email", value: "ivdtest2@test.com" }],
result = [];
data.forEach(function (a) {
if (!this[a.categoryName]) {
this[a.categoryName] = { _: [] };
result.push({ categoryId: a.categoryId, categoryName: a.categoryName, entity: this[a.categoryName]._ });
}
if (!this[a.categoryName][a.contactName]) {
this[a.categoryName][a.contactName] = [];
this[a.categoryName]._.push({ contactName: a.contactName, addresses: this[a.categoryName][a.contactName] });
}
this[a.categoryName][a.contactName].push({ name: a.name, value: a.value });
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }