lodash group by key code example
Example 1: lodash groupby return array
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
Example 2: lodash groupby return array
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);