为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

猛跑小猪 2022-12-02 17:23:00
我有以下数组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")我该怎么做?
查看完整描述

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);


查看完整回答
反对 回复 2022-12-02
?
紫衣仙女

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; }



查看完整回答
反对 回复 2022-12-02
?
ibeautiful

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; }


查看完整回答
反对 回复 2022-12-02
?
拉丁的传说

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));


查看完整回答
反对 回复 2022-12-02
  • 4 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信