gpt4 book ai didi

javascript - 通过带有下划线的多个属性对对象数组进行分组

转载 作者:行者123 更新时间:2023-11-28 18:19:29 25 4
gpt4 key购买 nike

我有这样的对象数组。他们有重复的属性(property)contactNamecategoryName值(value)观

[
{
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"
}
]
}]

}

]

最佳答案

var output = _.groupBy(input, function(entry) { 
return entry.categoryName + ',' + entry.contactName;
});

https://jsfiddle.net/9ttg1t3z/

关于javascript - 通过带有下划线的多个属性对对象数组进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com