猿问

如何获取数组中的过滤值和求和值?

我有以下数组


Array

(   [0] => GenderEn: "Male", ProvinceEn: "Chonburi"

    [1] => GenderEn: "Male", ProvinceEn: "Chonburi"

    [2] => GenderEn: "Female", ProvinceEn: "Chonburi"

    [3] => GenderEn: "Female", ProvinceEn: "Chonburi"

    [4] => GenderEn: "Male", ProvinceEn: "BKK"

    [5] => GenderEn: "Female", ProvinceEn: "BKK

)

然后我想从中创建这个数组:


Array

(   [0] => Male: "2", Female: "2" Sum : "4" Province : "Chonburi"

    [1] => Male: "1", Female: "1" Sum : "2" Province : "BKK"

)

我该怎么做?


猛跑小猪
浏览 122回答 4
4回答

喵喔喔

您可以使用一个对象来存储信息,以省份为键。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);

紫衣仙女

让我们为这个分解对象括号语法和对象获取器!由于您特别要求将数组作为输出,而不是对象: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; }

ibeautiful

您必须首先按键对数据进行分组,在本例中为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).lengthconst 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; }

拉丁的传说

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));
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答