【问题标题】:Javascript cumulate an array of object to an array of objectJavascript将对象数组累积到对象数组
【发布时间】:2017-01-04 15:28:00
【问题描述】:

我有一个对象数组:

var myArray = [
    {
        "date" : "03/01/2017",
        "value" : 2
    },  {
        "date" : "04/01/2017",
        "value" : 6
    },  {
        "date" : "05/01/2017",
        "value" : 4
    }
];

我需要累积值并使用更新的值保持相同的数组

结果应该是这样的

var myArray = [
    {
        "date" : "03/01/2017",
        "value" : 2
    },  {
        "date" : "04/01/2017",
        "value" : 8 //(2+6)
    },  {
        "date" : "05/01/2017",
        "value" : 12 //(2+6+4)
    }
];

我知道这是存在的

[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array) {
  return accumulator + currentValue;
});

但我找不到对象也返回对象的示例

【问题讨论】:

    标签: javascript arrays object


    【解决方案1】:

    使用Array.prototype.forEachthis 参数来累积 - 参见下面的演示:

    var myArray=[{"date":"03/01/2017","value":2},{"date":"04/01/2017","value":6},{"date":"05/01/2017","value":4}];
    
    myArray.forEach(function(e){
      this.count = (this.count || 0) +  e.value;
      e.value = this.count;
    },Object.create(null));
    
    console.log(myArray);
    .as-console-wrapper{top:0;max-height:100%!important;}

    【讨论】:

    • 为什么不只是 {} 而不是 Object.create(null) ?无论如何都是不错的方法
    • {} 也包括继承的原型...Object.create(null) 将避免它们...
    【解决方案2】:

    您可以使用map()Object.assign() 来复制对象。

    var myArray = [{
      "date": "03/01/2017",
      "value": 2
    }, {
      "date": "04/01/2017",
      "value": 6
    }, {
      "date": "05/01/2017",
      "value": 4
    }];
    
    var result = myArray.map(function(o) {
      var obj = Object.assign({}, o)
      obj.value = this.total += o.value
      return obj
    }, {total: 0})
    
    console.log(result)

    【讨论】:

      【解决方案3】:

      作为一个不同的例子并回答你在.reduce() 上的问题,你会以这种方式使用reduce

      var myArray = [
          {
              "date" : "03/01/2017",
              "value" : 2
          },  {
              "date" : "04/01/2017",
              "value" : 6
          },  {
              "date" : "05/01/2017",
              "value" : 4
          }
      ];
      
      function accumulate() {
        var count = 0;
        return myArray.reduce(function(acc, cur) {
          count += cur.value || 0;
          cur.value = count;
          acc.push(cur);
          return acc;
        }, []);
      }
      
      console.log(accumulate(myArray));

      【讨论】:

        【解决方案4】:

        简单的forEach 就地改变数组。

        var myArray = [
            {
                "date" : "03/01/2017",
                "value" : 2
            },  
            {
                "date" : "04/01/2017",
                "value" : 6
            },  
            {
                "date" : "05/01/2017",
                "value" : 4
            }
        ];
        
        myArray.forEach(
          (e,i,arr) => e.value += i && arr[i-1].value // only fires if i is truthy (i>0)
        );      
        
        console.log(myArray);
        .as-console-wrapper{top:0;max-height:100%!important;}

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-22
          相关资源
          最近更新 更多