使用lodash,我如何将具有相同生日的人的姓名分组如下所示?
我使用嵌套for循环编写了这个,但是我认为在lodash中会有更优雅的方法。我还没有使用它,并试图找出使用哪个功能。
[
{ "birthdate": "1993", "name": "Ben" },
{ "birthdate": "1994", "name": "John" },
{ "birthdate": "1995", "name": "Larry" },
{ "birthdate": "1995", "name": "Nicole" },
{ "birthdate": "1996", "name": "Jane" },
{ "birthdate": "1996", "name": "Janet" },
{ "birthdate": "1996", "name": "Dora" },
]
到
[
{ "birthdate": "1993", "names": [ "Ben" ] },
{ "birthdate": "1994", "names": [ "John"] },
{ "birthdate": "1995", "names": [ "Larry", "Nicole" ] },
{ "birthdate": "1996", "names": [ "Jane", "Janet", "Dora" ] }
]
答案 0 :(得分:8)
您可以使用groupBy()按birthdate
对集合中的每件商品进行分组。对项目进行分组后,您可以使用map()来迭代每个组,并将其形成为包含birthdate
和names
的新格式。要从分组的项目中获取一系列名称,您可以再次使用map()函数返回所有name
值。
var result = _(source)
.groupBy('birthdate')
.map(function(items, bdate) {
return {
birthdate: bdate,
names: _.map(items, 'name')
};
}).value();
var source = [
{ "birthdate": "1993", "name": "Ben" },
{ "birthdate": "1994", "name": "John" },
{ "birthdate": "1995", "name": "Larry" },
{ "birthdate": "1995", "name": "Nicole" },
{ "birthdate": "1996", "name": "Jane" },
{ "birthdate": "1996", "name": "Janet" },
{ "birthdate": "1996", "name": "Dora" },
];
var result = _(source)
.groupBy('birthdate')
.map(function(items, bdate) {
return {
birthdate: bdate,
names: _.map(items, 'name')
};
}).value();
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4) + '</pre>';
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
&#13;
答案 1 :(得分:1)
您可以链接groupBy和map来实现此目的。
_.chain(yourArray)
.groupBy('birthdate')
.toPairs()
.map(s=>{
return _.zipObject(['birthdate','names'],s)
})
.map(s=>{
s.names=_.map(s.names,x=>x.name);
return s;
})
.value()
答案 2 :(得分:0)
您可以使用reduce()
和find()
let arr = [{
"birthdate": "1993",
"name": "Ben"
},
{
"birthdate": "1994",
"name": "John"
},
{
"birthdate": "1995",
"name": "Larry"
},
{
"birthdate": "1995",
"name": "Nicole"
},
{
"birthdate": "1996",
"name": "Jane"
},
{
"birthdate": "1996",
"name": "Janet"
},
{
"birthdate": "1996",
"name": "Dora"
},
];
const res = arr.reduce((ac, a) => {
let temp = ac.find(x => x.birthdate === a.birthdate);
if (!temp) ac.push({ ...a,
name: [a.name]
})
else temp.name.push(a.name)
return ac;
}, [])
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
答案 3 :(得分:0)
一个简单且可自定义的纯javascript函数,用于返回按对象中所需属性分组的对象数组
export const groupArrayBy = (arr, groupBy) => {
let newArr = []
arr.map((item) => {
if(item[groupBy]) {
let finded = newArr.filter((newItem) => newItem[groupBy] === item[groupBy])
if(finded.length > 0) {
finded[0].products.push(item)
} else {
newArr.push({category: item[groupBy], products: [item]})
}
}
})
return newArr
}
答案 4 :(得分:0)
Lodash shortand
JComponent source = (JComponent)ae.getSource();
menu.show(source, source.getWidth()/2, source.getHeight()/2);
var data=[
{ "birthdate": "1993", "name": "Ben" },
{ "birthdate": "1994", "name": "John" },
{ "birthdate": "1995", "name": "Larry" },
{ "birthdate": "1995", "name": "Nicole" },
{ "birthdate": "1996", "name": "Jane" },
{ "birthdate": "1996", "name": "Janet" },
{ "birthdate": "1996", "name": "Dora" },
];
var list= _(data)
.groupBy('birthdate')
.map((items, birthdate)=> ({birthdate: birthdate,names: _.map(items, 'name')
})).value();
console.log(list)