Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按两个不同的键和和值对数组进行分组_Javascript_Arrays_Reactjs_Grouping - Fatal编程技术网

Javascript 按两个不同的键和和值对数组进行分组

Javascript 按两个不同的键和和值对数组进行分组,javascript,arrays,reactjs,grouping,Javascript,Arrays,Reactjs,Grouping,我正在使用React上的一个数组,我尝试按月份和年份对其进行筛选,我设法按月份进行筛选,但由于某些原因,我无法添加年份键,这是我迄今为止所拥有的: 这是我最初拥有的阵列: paid = [ {amount:155, month:11, year:2020, date:11-11-2020} {amount:120, month:11, year:2021, date:05-11-2021} {amount:135, month:12, year:2020, date:11-12-2020}

我正在使用React上的一个数组,我尝试按月份和年份对其进行筛选,我设法按月份进行筛选,但由于某些原因,我无法添加年份键,这是我迄今为止所拥有的:

这是我最初拥有的阵列:

paid = [
 {amount:155, month:11, year:2020, date:11-11-2020}
 {amount:120, month:11, year:2021, date:05-11-2021}
 {amount:135, month:12, year:2020, date:11-12-2020}
...
]

const group = groupBy(d, (invoices) => invoices.month); //d is the array called "paid"
这是groupBy函数:

function groupBy(list, keyGetter) {
    const map = new Map();
    list.forEach((item) => {
      const key = keyGetter(item);
      const collection = map.get(key);
      if (!collection) {
        map.set(key, [parseInt(item.amount)]);
      } else {
        collection.push(parseInt(item.amount));
      }
    });
    return map;
  }
这就是我得到的结果:

grouped = [
 {name:11, values: [155,120...]},
 {name:12, values: [135...]
];
我想让它也按年份分组,例如,月份11,不应该有两个值,因为在最初的数组中,我的月份是2020年的11,2021年的一个,所以我想要的结果是:

grouped = [
 {name:11/2020, values: [155...]},
 {name:11/2021, values: [120...]},
 {name:12/2020, values: [135...]
];

有人能帮我吗?

如果我理解正确,您希望每年每月对所有值求和。我想这会管用的

// Generate random data.
const genData = () => {
  const d = [];
  for(let i =0; i < 1000; i++) {
    d.push({
      year: Math.round(Math.random() * 10) + 2001,
      month: Math.round(Math.random() * 12),
      amount: Math.round(Math.random() * 100) + 100
    })
  }
  return d;
};

// Sum all data per month per year
const sumData = (d) => {
  const summed = {};
  for(const {year,month,amount} of d) {
    // By using a unique key for each year/month combi you can easily access the
    // intermedeiate result and add another value.
    const key = year + '/' + month;
    
    // If it does not yet exist, create an empty record for this year/month
    if(!(key in summed)) {
      summed[key] = {year,month, sum:0, values:[]};
    }
    
    // Grab the intermediate values and add amount to sum and to values
    summed[key].values.push(amount)
    summed[key].sum += amount;
  }
  return Object.values(summed);
};

// Run all
const d = genData();
console.log(d);
console.log(sumData(d));
//生成随机数据。
恒性别=()=>{
常数d=[];
for(设i=0;i<1000;i++){
d、 推({
年份:Math.round(Math.random()*10)+2001,
月份:Math.round(Math.random()*12),
金额:Math.round(Math.random()*100)+100
})
}
返回d;
};
//每年每月汇总所有数据
const sumData=(d)=>{
const summated={};
(d的常数{年、月、金额}){
//通过使用每年/每月组合的唯一键,您可以轻松访问
//中间化结果并添加另一个值。
常量键=年+'/'+月;
//如果该记录尚不存在,请为此年/月创建一个空记录
如果(!(输入总和)){
求和[键]={年、月、和:0,值:[]};
}
//抓取中间值并将金额添加到总和和值中
求和[key]。值。推送(金额)
求和[键]。求和+=金额;
}
返回Object.value(求和);
};
//全部运行
常数d=性别();
控制台日志(d);
console.log(sumData(d));

如果我理解正确,您希望将每年每月的所有值相加。我想这会管用的

// Generate random data.
const genData = () => {
  const d = [];
  for(let i =0; i < 1000; i++) {
    d.push({
      year: Math.round(Math.random() * 10) + 2001,
      month: Math.round(Math.random() * 12),
      amount: Math.round(Math.random() * 100) + 100
    })
  }
  return d;
};

// Sum all data per month per year
const sumData = (d) => {
  const summed = {};
  for(const {year,month,amount} of d) {
    // By using a unique key for each year/month combi you can easily access the
    // intermedeiate result and add another value.
    const key = year + '/' + month;
    
    // If it does not yet exist, create an empty record for this year/month
    if(!(key in summed)) {
      summed[key] = {year,month, sum:0, values:[]};
    }
    
    // Grab the intermediate values and add amount to sum and to values
    summed[key].values.push(amount)
    summed[key].sum += amount;
  }
  return Object.values(summed);
};

// Run all
const d = genData();
console.log(d);
console.log(sumData(d));
//生成随机数据。
恒性别=()=>{
常数d=[];
for(设i=0;i<1000;i++){
d、 推({
年份:Math.round(Math.random()*10)+2001,
月份:Math.round(Math.random()*12),
金额:Math.round(Math.random()*100)+100
})
}
返回d;
};
//每年每月汇总所有数据
const sumData=(d)=>{
const summated={};
(d的常数{年、月、金额}){
//通过使用每年/每月组合的唯一键,您可以轻松访问
//中间化结果并添加另一个值。
常量键=年+'/'+月;
//如果该记录尚不存在,请为此年/月创建一个空记录
如果(!(输入总和)){
求和[键]={年、月、和:0,值:[]};
}
//抓取中间值并将金额添加到总和和值中
求和[key]。值。推送(金额)
求和[键]。求和+=金额;
}
返回Object.value(求和);
};
//全部运行
常数d=性别();
控制台日志(d);
console.log(sumData(d));

使用
数组。reduce
方法可能更简单,例如我添加了一些值:

const paid=[
{金额:155,月份:11,年份:2020,日期:11-11-2020},
{金额:160,月份:11,年份:2020,日期:11-11-2020},
{金额:120,月份:11,年份:2021,日期:05-11-2021},
{金额:130,月份:11,年份:2021,日期:05-11-2021},
{金额:135,月份:12,年份:2020,日期:11-12-2020},
{金额:145,月份:12,年份:2020,日期:11-12-2020}
]
const GROUBLED=已支付。减少((acc,val)=>{
如果(根据[价值月+'/'+价值年]){
acc[val.month+'/'+val.year]。推送(val.amount)
}否则{
会计科目[val.MOUNT+'/'+val.year]=[val.amount]
}
返回acc
}, {})

console.log(JSON.stringify(grouped,null,2))
使用
Array.reduce
方法可能更简单,例如我添加了一些值:

const paid=[
{金额:155,月份:11,年份:2020,日期:11-11-2020},
{金额:160,月份:11,年份:2020,日期:11-11-2020},
{金额:120,月份:11,年份:2021,日期:05-11-2021},
{金额:130,月份:11,年份:2021,日期:05-11-2021},
{金额:135,月份:12,年份:2020,日期:11-12-2020},
{金额:145,月份:12,年份:2020,日期:11-12-2020}
]
const GROUBLED=已支付。减少((acc,val)=>{
如果(根据[价值月+'/'+价值年]){
acc[val.month+'/'+val.year]。推送(val.amount)
}否则{
会计科目[val.MOUNT+'/'+val.year]=[val.amount]
}
返回acc
}, {})

console.log(JSON.stringify(grouped,null,2))
然后调整/fix
keyGetter()
,这样它返回一个由年和月组成的值。如果我做这个const group=groupBy(d,(invoices)=>[invoices.month,invoices.year]);我得到的结果是:grouped=[{[name:'11','2020'],[value:'155','155','11/11/2020']}…]和所有分开的寄存器,它们不分组,它为每个寄存器创建一个数组,以月份和年份作为一个名为name的数组。这个数组可以用于对象,但不能用于
映射。将键设为字符串就完成了。然后调整/fix
keyGetter()
,使其返回一个由年和月组成的值。如果我这样做,const group=groupBy(d,(invoices)=>[invoices.month,invoices.year]);我得到的结果是:grouped=[{[name:'11','2020'],[value:'155','155','11/11/2020']}…]和所有分开的寄存器,它们不分组,它为每个寄存器创建一个数组,以月份和年份作为一个名为name的数组。这个数组可以用于对象,但不能用于
映射。是的,我实际上是在使用reduce to to to to the sum,但我没有把它放在帖子中,因为这不是我一直在努力解决的问题,但是谢谢你的回答!您好@delv123我刚刚编辑了我的答案,以更好地满足您的需要是的,我实际上是在使用“减少到总和”,但我没有把它放在帖子中,因为这不是我一直在努力解决的问题,但感谢您的回答!您好@delv123我刚刚编辑了我的答案,以更好地满足您的需要。非常感谢,这非常有效!非常感谢你,这非常有效!