鍍金池/ 問答/HTML/ js 如何把salary數(shù)組中 postDate 是相同月份的 amountMo

js 如何把salary數(shù)組中 postDate 是相同月份的 amountMoney相加 返回新的數(shù)組?

{
"salary": [

{
  "postDate": "2017-09-29 00:00:00",
  "amountMoney": "495.0"
},
{
  "postDate": "2017-09-13 00:00:00",
  "amountMoney": "495.0"
},
{
  "postDate": "2017-09-04 00:00:00",
  "amountMoney": "2526.06"
},
{
  "postDate": "2017-08-08 00:00:00",
  "amountMoney": "495.0"
},
{
  "postDate": "2017-08-08 00:00:00",
  "amountMoney": "2526.06"
},
{
  "postDate": "2017-07-24 00:00:00",
  "amountMoney": "495.0"
},
{
  "postDate": "2017-07-19 00:00:00",
  "amountMoney": "2526.06"
},
{
  "postDate": "2017-06-16 00:00:00",
  "amountMoney": "2772.06"
}

]
}

回答
編輯回答
玩控

array.map()

2017年9月3日 08:15
編輯回答
不歸路

主要是不知道怎么把 月份數(shù)相同的amountMoney相加 也就是map()里面的內(nèi)容。。

2018年1月17日 03:28
編輯回答
誮惜顏

把postDate參數(shù)轉(zhuǎn)換為 時間戳 進行相等匹配 再返回新數(shù)組

2017年12月28日 18:40
編輯回答
司令
function group (data) {
  let cache = {}
  data.forEach(item => {
    let month = new Date(item.postDate).getMonth() + 1 + ''
    if (month.length === 1) month = '0' + month
    if (cache[month]) {
      cache[month] += +item.amountMoney
    } else {
      cache[month] = +item.amountMoney
    }
  })
  let result = []
  for (let key in cache) {
    result.push({postDate: key, amountMoney: '' + cache[key]})
  }
  return result
}

group([{
  "postDate": "2017-09-29 00:00:00",
  "amountMoney": "495.0"
}, {
  "postDate": "2017-09-13 00:00:00",
  "amountMoney": "495.0"
}, {
  "postDate": "2017-09-04 00:00:00",
  "amountMoney": "2526.06"
}, {
  "postDate": "2017-08-08 00:00:00",
  "amountMoney": "495.0"
}, {
  "postDate": "2017-08-08 00:00:00",
  "amountMoney": "2526.06"
}, {
  "postDate": "2017-07-24 00:00:00",
  "amountMoney": "495.0"
}, {
  "postDate": "2017-07-19 00:00:00",
  "amountMoney": "2526.06"
}, {
  "postDate": "2017-06-16 00:00:00",
  "amountMoney": "2772.06"
}])

不知道滿不滿足你的需求,因為按月份歸并,所以返回的postDate顯示的是月份,結(jié)果如下:

clipboard.png

2018年1月17日 01:57
編輯回答
舊螢火
var salary = [
    {
      "postDate": "2017-09-29 00:00:00",
      "amountMoney": "495.0"
    },
    {
      "postDate": "2017-09-13 00:00:00",
      "amountMoney": "495.0"
    },
    {
      "postDate": "2017-09-04 00:00:00",
      "amountMoney": "2526.06"
    },
    {
      "postDate": "2017-08-08 00:00:00",
      "amountMoney": "495.0"
    },
    {
      "postDate": "2017-08-08 00:00:00",
      "amountMoney": "2526.06"
    },
    {
      "postDate": "2017-07-24 00:00:00",
      "amountMoney": "495.0"
    },
    {
      "postDate": "2017-07-19 00:00:00",
      "amountMoney": "2526.06"
    },
    {
      "postDate": "2017-06-16 00:00:00",
      "amountMoney": "2772.06"
    }
];
var rs = [];
var json = {};
for (let i = 0, len = salary.length; i < len; i++) {
    var month = salary[i].postDate.split('-')[1];
    if (json[month] !== 1) {
        rs.push ({
            month: month,
            amountMoney: Number(salary[i].amountMoney)
        })
        json[month] = 1;
    } else {
        for (let j = 0, l = rs.length; j < l; j++) {
            if (rs[j].month == month) {
                rs[j].amountMoney += Number(salary[i].amountMoney)
            }
        }
    }
}
console.log(rs);
2018年3月13日 23:22