如何在不创建新条目的情况下将值推送到子数组中

时间:2018-03-21 15:10:03

标签: javascript arrays typescript multidimensional-array

所以我有这两个数组:

resultList: { date: string, amount: number }[] = [];
dateList: { date: string, amounts: { amount: number }[] }[] = [];

其中一个有所有结果,我想按日期排序,因此第二个数组。 这是我用来尝试实现此目的的代码:

this.resultList.forEach((result) => {
      let dateFound: boolean = false;
      this.dateList.forEach((date) => {
        if (result.date === date.date) {
          dateFound = true;
          return;
        }
      });
      if (dateFound == false) {
        //create new date entry in dateList
        this.dateList.push({date: result.date, amounts: []});
      }
      //find that date entry and push a value to it's sub array called amounts
      this.dateList.find((dates) => {
        return dates.date == result.date
      }).amounts.push({
        amount: result.amount
      });
    });

如果您有3个相同日期的结果,则输出

[
  {date: '2018-03-21', amounts: [{amount: 1}]},
  {date: '2018-03-21', amounts: [{amount: 1},{amount: 43}]},
  {date: '2018-03-21', amounts: [{amount: 1},{amount: 43}, {amount: 55}]}
]

如果您有3个相同日期的结果,则需要输出

[
  {date: '2018-03-21', amounts: [{amount: 1},{amount: 43}, {amount: 55}]}
]

3 个答案:

答案 0 :(得分:1)

我在if条件中添加了else子句并删除了.find()部分:

if (dateFound == false) {
  //create new date entry in dateList
  this.dateList.push({date: result.date, amounts: []});
} else {
  for (let d of this.dateList) {
    if (d.date == result.date) {
      d.amounts.push({amount: result.amount})
    }
  }
}

答案 1 :(得分:1)

您可以先将reducing数据放入一个对象中,将唯一日期作为键,将金额作为每个键的值,然后mapping将它们放入您想要的结构中输出,如下:



var data = [
  {date: '2018-03-21', amount: 1},
  {date: '2018-03-21', amount: 43},
  {date: '2018-03-21', amount: 41},
  {date: '2018-03-22', amount: 18},
  {date: '2018-03-23', amount: 25},
  {date: '2018-03-24', amount: 15},
  {date: '2018-03-24', amount: 25},
];

// reduce to single object with unique dates as keys, collection of amounts as values
var dateMap = data.reduce((res, curr) => {
  // if the date has not been added already
  if (!res[curr.date]) {
    // create it on the result object
    res[curr.date] = []
  }
  // push the amount into the array for the date
  res[curr.date].push({amount: curr.amount});
  return res;
}, {});

// map each key of dateMap to an object matching the desired output format
var dateList = Object.keys(dateMap).map(key => {
  return {date: key, amounts: dateMap[key]};
});

console.log(dateList);




答案 2 :(得分:0)

您可以将reduce数组转换为Map。对于每个日期,创建一个具有amounts属性的对象,并使用相同日期的金额值填充它。然后spread Map.values()返回数组:



const data = [{"date":"2018-03-21","amount":1},{"date":"2018-03-21","amount":43},{"date":"2018-03-21","amount":41},{"date":"2018-03-22","amount":18},{"date":"2018-03-23","amount":25},{"date":"2018-03-24","amount":15},{"date":"2018-03-24","amount":25}];

const result = [...data
  .reduce((r, o) => {
    const { date, amount } = o;
  
    r.has(date) || r.set(date, { 
      date, 
      amounts: [] 
    });
    
    r.get(date).amounts.push({ amount });
    
    return r;
  }, new Map())
  .values()
];

console.log(result);