4 回答
TA贡献1735条经验 获得超5个赞
您可以使用一个对象来存储信息,以省份为键。
const arr = [
{GenderEn: "Male", ProvinceEn: "Chonburi"},
{GenderEn: "Male", ProvinceEn: "Chonburi"},
{GenderEn: "Female", ProvinceEn: "Chonburi"},
{GenderEn: "Female", ProvinceEn: "Chonburi"},
{GenderEn: "Male", ProvinceEn: "BKK"},
{GenderEn: "Female", ProvinceEn: "BKK"}
];
var res = Object.values(
arr.reduce((acc,{GenderEn: gender, ProvinceEn: Province})=>{
const p = acc[Province] = acc.hasOwnProperty(Province)?acc[Province]: {Male: 0, Female: 0, Sum: 0, Province};
p[gender]++, p.Sum++;
return acc;
}, {})
);
console.log(res);
TA贡献1839条经验 获得超15个赞
让我们为这个分解对象括号语法和对象获取器!
由于您特别要求将数组作为输出,而不是对象:
const data = [
{GenderEn: "Male", ProvinceEn: "Chonburi"},
{GenderEn: "Male", ProvinceEn: "Chonburi"},
{GenderEn: "Female", ProvinceEn: "Chonburi"},
{GenderEn: "Female", ProvinceEn: "Chonburi"},
{GenderEn: "Male", ProvinceEn: "BKK"},
{GenderEn: "Female", ProvinceEn: "BKK"}
];
var output = [];
for (entry of data) {
let el = output.find(e => e.Province == entry.ProvinceEn);
if (el) { //If output does contain province entry, add data to it
el[entry.GenderEn]++;
}
else { //Else, create it
output.push({
Male: entry.GenderEn == 'Male' ? 1 : 0,
Female: entry.GenderEn == 'Female' ? 1 : 0,
get Sum() { //If sum is a getter, you'll never have to set it manually
return this.Male + this.Female;
},
Province: entry.ProvinceEn,
});
}
}
console.log(output);
.as-console-wrapper { top: 0; max-height: 100% !important; }
TA贡献1993条经验 获得超5个赞
您必须首先按键对数据进行分组,在本例中为ProvinceEn。拥有组后,您可以将统计信息映射到新对象。
我提供了通用的分组和汇总功能。
const rawData = [
{ "GenderEn": "Male" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Male" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Female" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Female" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Male" , "ProvinceEn": "BKK" },
{ "GenderEn": "Female" , "ProvinceEn": "BKK" }
]
const count = (data, key, value) =>
data.filter(item => item[key] === value).length
const groupBy = (data, key) =>
data.reduce((groups, item) =>
(existingArr => ({...groups, [item[key]]: [...existingArr, item]}))
(groups[item[key]] || []), {})
const summarize = (data, groupKey, mapper) =>
(groups => Object.entries(groups).map(entry => mapper.apply(null, entry)))
(groupBy(data, groupKey))
console.log(summarize(rawData, 'ProvinceEn', (key, data) => ({
'Male' : count(data, 'GenderEn', 'Male'),
'Female' : count(data, 'GenderEn', 'Female'),
'Sum' : data.length,
'ProvinceEn' : key
})))
.as-console-wrapper { top: 0; max-height: 100% !important; }
如果上面的代码是整体式的,这就是它的样子。它非常像 LISP。;)
const rawData = [
{ "GenderEn": "Male" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Male" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Female" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Female" , "ProvinceEn": "Chonburi" },
{ "GenderEn": "Male" , "ProvinceEn": "BKK" },
{ "GenderEn": "Female" , "ProvinceEn": "BKK" }
]
const summary = (groups =>
Object.keys(groups).map(key => ({
'Male' : groups[key].filter(item => item['GenderEn'] === 'Male').length,
'Female' : groups[key].filter(item => item['GenderEn'] === 'Female').length,
'Sum' : groups[key].length,
'ProvinceEn' : key
})))
(rawData.reduce((groups, item) =>
(existingArr => ({...groups, [item['ProvinceEn']]: [...existingArr, item]}))
(groups[item['ProvinceEn']] || []), {}), 'ProvinceEn')
console.log(summary)
.as-console-wrapper { top: 0; max-height: 100% !important; }
TA贡献1789条经验 获得超8个赞
const test = [
{GenderEn: "Male", ProvinceEn: "Chonburi"},
{GenderEn: "Male", ProvinceEn: "Chonburi"},
{GenderEn: "Female", ProvinceEn: "Chonburi"},
{GenderEn: "Female", ProvinceEn: "Chonburi"},
{GenderEn: "Male", ProvinceEn: "BKK"},
{GenderEn: "Female", ProvinceEn: "BKK"}
]
function getProvinceSum(arr){
return arr.reduce((output, arrObj) => {
if(!output.some(outputObj => outputObj.Province == arrObj.ProvinceEn)){
output.push({
Male: arrObj.GenderEn == "Male" ? 1 : 0,
Female: arrObj.GenderEn == "Female" ? 1 : 0,
Sum: 1,
Province: arrObj.ProvinceEn
});
}else{
const outputObj = output.find(outputObj => outputObj.Province == arrObj.ProvinceEn);
arrObj.GenderEn == "Male" ? outputObj.Male += 1 : outputObj.Female += 1;
outputObj.Sum += 1;
}
return output;
},[]);
}
console.log(getProvinceSum(test));
添加回答
举报