我试图独自解决问题,但没有设法解决。所以我决定寻求帮助。
我有一个这样的JSON对象数组:
const objArr = [
{
name: 'Andrew',
city: 'London'
},
{
name: 'Edouard',
city: 'Paris'
},
{
name: 'Nathalie',
city: 'London'
},
{
name: 'Patrick',
city: 'London'
},
{
name: 'Mathieu',
city: 'Paris'
}
];
我想在一个新数组中收集具有相同键值(在这种情况下为城市键)的对象以获取此值:
const newObjArr = [
[{
name: 'Andrew',
city: 'London'
},
{
name: 'Nathalie',
city: 'London'
},
{
name: 'Patrick',
city: 'London'
}],
[{
name: 'Edouard',
city: 'Paris'
},
{
name: 'Mathieu',
city: 'Paris'
}]
];
答案 0 :(得分:2)
这是.reduce()
的工作。
const objArr = [
{name: 'Andrew', city: 'London'},
{name: 'Edouard', city: 'Paris'},
{name: 'Nathalie', city: 'London'},
{name: 'Patrick', city: 'London'},
{name: 'Mathieu', city: 'Paris'}
];
// Object of arrays
const result = objArr.reduce((acc, obj) => {
return {...acc, [obj.city]: [...acc[obj.city] || [], obj]}
}, {})
// Array of arrays
const result2 = Object.values(result);
console.log(result2)
答案 1 :(得分:1)
您可以使用reduce对该字段进行分组(使用该字段作为键),然后如果确实只需要这些值,则使用Object.values:
const objArr = [ { name: 'Andrew', city: 'London' }, { name: 'Edouard', city: 'Paris' }, { name: 'Nathalie', city: 'London' }, { name: 'Patrick', city: 'London' }, { name: 'Mathieu', city: 'Paris' } ];
var groupBy = function(array, k) {
return array.reduce(function(acc, cur) {
(acc[cur[k]] = acc[cur[k]] || []).push(cur);
return acc;
}, {});
};
console.log(Object.values(groupBy(objArr, 'city')));
答案 2 :(得分:1)
使用lodash group by,然后将其添加到新数组
var objArr = [ { name: 'Andrew', city: 'London' }, { name: 'Edouard', city: 'Paris' }, { name: 'Nathalie', city: 'London' }, { name: 'Patrick', city: 'London' }, { name: 'Mathieu', city: 'Paris' } ]
var grouped = _.mapValues(_.groupBy(objArr, 'city'),
clist => clist.map(city => _.omit(city, 'city')));
var result=[]
for (const [key, value] of Object.entries(grouped)) {
var array=[]
value.forEach(x=>{
array.push({ name: x.name, city:key })
})
result.push(array);
}
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
答案 3 :(得分:0)
const objArr = [
{
name: "Andrew",
city: "London",
},
{
name: "Edouard",
city: "Paris",
},
{
name: "Nathalie",
city: "London",
},
{
name: "Patrick",
city: "London",
},
{
name: "Mathieu",
city: "Paris",
},
];
// Gather all the different cities
let allCities = [];
objArr.forEach( _x => {
let city = _x.city;
if (! allCities.includes(city)) {
allCities.push(city);
};
});
// Format 1
/*
{
London: [
{ name: 'Andrew', city: 'London' },
{ name: 'Nathalie', city: 'London' },
{ name: 'Patrick', city: 'London' }
],
Paris: [
{ name: 'Edouard', city: 'Paris' },
{ name: 'Mathieu', city: 'Paris' }
]
}
*/
let newObjArr1 = {};
allCities.forEach( _city => {
newObjArr1[_city] = objArr.filter( _x => {
return _x.city === _city
});
});
console.log( newObjArr1 );
// Format 2
/*
[
[
{ name: 'Andrew', city: 'London' },
{ name: 'Nathalie', city: 'London' },
{ name: 'Patrick', city: 'London' }
],
[
{ name: 'Edouard', city: 'Paris' },
{ name: 'Mathieu', city: 'Paris' }
]
]
*/
let newObjArr2 = [];
for (let _i = 0; _i < allCities.length; _i ++) {
let _city = allCities[_i];
newObjArr2[_i] = objArr.filter( _x => {
return _x.city === _city
});
};
console.log( newObjArr2 );