在减少值上保留先前的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在减少值上保留先前的值相关的知识,希望对你有一定的参考价值。

我正在尝试创建汽车的模型年份数组,因此模型年份是此处的唯一键,如果存在,则应仅将其添加到totalMilage中。

我知道如何使用年作为年份数组的标识符,但是在这种特定情况下,我只需要一个常规标识符[0, 1, 2]

这是我想做的事情和我开始做的事情

const data = [
    year: 2001,
    car: 'Audi',
    nrOfDoors: 5
  ,
  
    year: 2010,
    car: 'Audi',
    nrOfDoors: 3,
    milage: 2000
  , 
    year: 2019,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2005,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2009,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2002,
    car: 'Audi',
    nrOfDoors: 4,
    milage: 2000
  , 
    year: 2001,
    car: 'Audi',
    nrOfDoors: 3,
    milage: 2000
  , 
    year: 2001,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  
]

const newList = data.reduce((acc, cur, idx) => 
  // try to keep track of previous year, previourYear below
  // create one array for unique years and add the milage to total
  // create array with this structure

  // if (previousYear !== cur.year) 
  //  acc = 
  //    year: cur.year,
  //    totalMilage: 0
  //  
  // 



  return acc
, [])

这是“命名”数组的处理方法

const data = [
    year: 2001,
    car: 'Audi',
    nrOfDoors: 5
  ,
  
    year: 2010,
    car: 'Audi',
    nrOfDoors: 3,
    milage: 2000
  , 
    year: 2019,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2005,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2009,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2002,
    car: 'Audi',
    nrOfDoors: 4,
    milage: 2000
  , 
    year: 2001,
    car: 'Audi',
    nrOfDoors: 3,
    milage: 2000
  , 
    year: 2001,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  
]

let newList = data.reduce((accumulator, currentValue) => 
  const key = currentValue.year

  if (accumulator[key] === undefined) 
    accumulator[key] = 
      year: key,
      totalMilage: 0
    
  
  
  accumulator[key].totalMilage += accumulator[key].totalMilage + currentValue.milage
  
  return accumulator
, [])
答案

我建议使用对象来更轻松地跟踪您到目前为止所看的年份。然后,如果需要,可以使用Object.entries将对象还原为数组。

const data = [
    year: 2001,
    car: 'Audi',
    nrOfDoors: 5
  ,
  
    year: 2010,
    car: 'Audi',
    nrOfDoors: 3,
    milage: 2000
  , 
    year: 2019,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2005,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2009,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  , 
    year: 2002,
    car: 'Audi',
    nrOfDoors: 4,
    milage: 2000
  , 
    year: 2001,
    car: 'Audi',
    nrOfDoors: 3,
    milage: 2000
  , 
    year: 2001,
    car: 'Audi',
    nrOfDoors: 5,
    milage: 2000
  
];

const summed = data.reduce((acc, el) => 
  if(!acc[el.year]) 
    acc[el.year] = 0;
  
  acc[el.year] += el.milage;
  return acc;
, )

console.log(summed);

const summedArr = Object.entries(summed).reduce((acc, item) => 
  acc.push( year: item[0], milage: item[1] );
  return acc;
, [])

console.log(summedArr);
另一答案

[如果您只是想按年份获取总里程数,则可以这样做:

const data = [
    
        year: 2001,
        car: 'Audi',
        nrOfDoors: 5,
    ,
    
        year: 2010,
        car: 'Audi',
        nrOfDoors: 3,
        milage: 2000,
    ,
    
        year: 2019,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    ,
    
        year: 2005,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    ,
    
        year: 2009,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    ,
    
        year: 2002,
        car: 'Audi',
        nrOfDoors: 4,
        milage: 2000,
    ,
    
        year: 2001,
        car: 'Audi',
        nrOfDoors: 3,
        milage: 2000,
    ,
    
        year: 2001,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    ,
];

const newList = data.reduce((acc, d) => 
    if(!acc[d.year]) 
        acc[d.year] = 0;
    

    acc[d.year] += d.milage ? d.milage : 0;

    return acc;
, );

[基本上,如果累加器对象还没有该年份,则只需创建它并将其设置为零。然后,按年份将当前对象的里程添加到我们的累加器中。

以上是关于在减少值上保留先前的值的主要内容,如果未能解决你的问题,请参考以下文章

自动释放对象递减的保留计数何时减少?

React JS:setState中的previousState在状态更新后不保留先前状态的数据

保留文本框的先前值,直到表单关闭

重新加载页面后如何保留变量的值? [复制]

暂时恢复到先前的提交,进行更改,保留更改并返回到最新的提交?

赛普拉斯 - addContext()保留先前的失败计数并将其添加到mochawesome报告中的每个'it'场景中