我有一个这样的数据集:
[ { city: 'a', value: 1, sector: 'Hospital' },
{ city: 'b', value: 1, sector: 'Hardware' },
{ city: 'c', value: 1, sector: 'Hardware' },
{ city: 'd', value: 1, sector: 'Networking' },
{ city: 'e', value: 1, sector: 'Hospital' },
{ city: 'f', value: 1, sector: 'Education' },
{ city: 'g', value: 1, sector: 'Transport' },
{ city: 'h', value: 1, sector: 'Food' },
{ city: 'i', value: 1, sector: 'Networking' },
{ city: 'j', value: 0.7, sector: 'Software' },
{ city: 'k', value: 0.7, sector: 'Education' },
{ city: 'l', value: 0.7, sector: 'Food' },
{ city: 'm', value: 0.7, sector: 'Hospital' },
{ city: 'n', value: 0.2, sector: 'Networking' },
{ city: 'o', value: 0.2, sector: 'Networking' },
{ city: 'p', value: 0.2, sector: 'Industrial' },
{ city: 'q', value: 0.2, sector: 'Transport' },
{ city: 'r', value: 0.2, sector: 'Software' } ]现在我想用数组将对象数组转换成对象,具有相同扇区重复的城市应该属于特定的组。
期望的输出如下:
[
{
sector: "Hospital",
place: ["a", "e", "m"]
},
{
sector: "Hardware",
place: ["b", "c"]
},
{
sector: "Networking",
place: ["d", "i", "n", "o"]
},
{
sector: "Education",
place: ["f", "k"]
},
{
sector: "Transport",
place: ["g", "q"]
},
{
sector: "Food",
place: ["h", "l"]
},
{
sector: "Software",
place: ["j", "r"]
},
{
sector: "Industrial",
place: ["q"]
},
]有没有人建议我怎么做这种类型的任务。任何帮助或建议都是非常感谢的。
我有一个线索,这件事将通过reduce和map函数来完成,但如何实现将是一个挑战。
我试着像这样获得扇区重复计数,但没有得到我想要的输出:
let x = data.reduce((m, c) => {
if (c.sector in m) m[c.sector].count += 1;
else m[c.sector] = { sector: c.sector, count: 1 };
return m;
}, {});
console.log(x)发布于 2018-12-11 21:55:14
您可以首先提取array中的所有uniq地段。
之后将map放入每个扇区,并将filter放入您的数据所对应的位置。
var data = [{ city: 'a', value: 1, sector: 'Hospital' },
{ city: 'b', value: 1, sector: 'Hardware' },
{ city: 'c', value: 1, sector: 'Hardware' },
{ city: 'd', value: 1, sector: 'Networking' },
{ city: 'e', value: 1, sector: 'Hospital' },
{ city: 'f', value: 1, sector: 'Education' },
{ city: 'g', value: 1, sector: 'Transport' },
{ city: 'h', value: 1, sector: 'Food' },
{ city: 'i', value: 1, sector: 'Networking' },
{ city: 'j', value: 0.7, sector: 'Software' },
{ city: 'k', value: 0.7, sector: 'Education' },
{ city: 'l', value: 0.7, sector: 'Food' },
{ city: 'm', value: 0.7, sector: 'Hospital' },
{ city: 'n', value: 0.2, sector: 'Networking' },
{ city: 'o', value: 0.2, sector: 'Networking' },
{ city: 'p', value: 0.2, sector: 'Industrial' },
{ city: 'q', value: 0.2, sector: 'Transport' },
{ city: 'r', value: 0.2, sector: 'Software' } ];
var sectors = [];
data.map(d => {
if(sectors.indexOf(d.sector) === -1){
sectors.push(d.sector);
}
})
sectors = sectors.map(sector => {
return {
sector,
places:data.filter(d => d.sector === sector).map(d => d.city)
}
});
console.log(sectors)
发布于 2018-12-11 22:12:06
与findIndex对reduce的使用略有不同。不需要以这种方式创建单独的键数组。
const data = [{"city":"a","value":1,"sector":"Hospital"},{"city":"b","value":1,"sector":"Hardware"},{"city":"c","value":1,"sector":"Hardware"},{"city":"d","value":1,"sector":"Networking"},{"city":"e","value":1,"sector":"Hospital"},{"city":"f","value":1,"sector":"Education"},{"city":"g","value":1,"sector":"Transport"},{"city":"h","value":1,"sector":"Food"},{"city":"i","value":1,"sector":"Networking"},{"city":"j","value":0.7,"sector":"Software"},{"city":"k","value":0.7,"sector":"Education"},{"city":"l","value":0.7,"sector":"Food"},{"city":"m","value":0.7,"sector":"Hospital"},{"city":"n","value":0.2,"sector":"Networking"},{"city":"o","value":0.2,"sector":"Networking"},{"city":"p","value":0.2,"sector":"Industrial"},{"city":"q","value":0.2,"sector":"Transport"},{"city":"r","value":0.2,"sector":"Software"}];
const out = data.reduce((acc, c) => {
// Grab the sector and city
const { sector, city } = c;
// Find an object in the array that matches the sector
const found = acc.findIndex(el => el.sector === sector);
// If it exists...
if (found > -1) {
// ...push a new city to the place array
acc[found].place.push(city);
} else {
// ...otherwise push a new object to the array
// with the starter city
acc.push({ sector, place: [city] });
}
return acc;
}, []);
console.log(out);
发布于 2018-12-11 22:05:04
您可以创建一个函数,该函数将使用reduce对数组进行迭代,并将唯一扇区存储在数组中,然后使用map和filter返回包含所需值的数组。
function normalize (input){
const sectors = input.reduce(function(result, value){
if(result.indexOf(value.sector) === -1){
result.push(value.sector);
}
return result;
}, []);
return sectors.map(function(sector){
return {
sector,
places: input.filter(function(entry){
return entry.sector === sector;
}).map(function(entry){
return entry.city;
})
}
});
}https://stackoverflow.com/questions/53725460
复制相似问题