javascript - Javascript - ES6 :如何将对象数组简化为唯一值?

如何减少此数组:


const array = [


 {'location':"Plovdiv", 'department':"Finance"},


 {'location':"Plovdiv", 'department':"Client & Employee Support"},


 {'location':"Plovdiv", 'department':"Client & Employee Support"},


 {'location':"London", 'department':"Engineering"},


 {'location':"London", 'department':"Engineering"},


 {'location':"Plovdiv", 'department':"Engineering"}


];



变成这个:


{'location':"Plovdiv", 'department': ["Finance","Client & Employee Support","Engineering"]},


{'location':"London", 'department': ["Engineering"]},



时间: 原作者:

使用Setmap


const array = [


 {'location':"Plovdiv", 'department':"Finance"},


 {'location':"Plovdiv", 'department':"Client & Employee Support"},


 {'location':"Plovdiv", 'department':"Client & Employee Support"},


 {'location':"London", 'department':"Engineering"},


 {'location':"London", 'department':"Engineering"},


 {'location':"Plovdiv", 'department':"Engineering"}


];



const unique = new Array(...new Set(array.map(({ location }) => location)))


 .map(location => ({ 


 location,


 department: array.filter(({ location: l}) => location === l)


 .map(({ department }) => department)


 }));



console.log(unique);



 

原作者:
...