js 如何把salary数组中 postDate 是相同月份的 amountMoney相加 返回新的

{

"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"

}

]

}


繁星coding
浏览 646回答 5
5回答

慕姐8265434

var salary = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-09-29 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "495.0"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-09-13 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "495.0"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-09-04 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "2526.06"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-08-08 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "495.0"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-08-08 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "2526.06"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-07-24 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "495.0"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-07-19 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "2526.06"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "postDate": "2017-06-16 00:00:00",&nbsp; &nbsp; &nbsp; "amountMoney": "2772.06"&nbsp; &nbsp; }];var rs = [];var json = {};for (let i = 0, len = salary.length; i < len; i++) {&nbsp; &nbsp; var month = salary[i].postDate.split('-')[1];&nbsp; &nbsp; if (json[month] !== 1) {&nbsp; &nbsp; &nbsp; &nbsp; rs.push ({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month: month,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; amountMoney: Number(salary[i].amountMoney)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; json[month] = 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; for (let j = 0, l = rs.length; j < l; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rs[j].month == month) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs[j].amountMoney += Number(salary[i].amountMoney)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}console.log(rs);

Qyouu

array.map()

鸿蒙传说

主要是不知道怎么把 月份数相同的amountMoney相加 也就是map()里面的内容。。

UYOU

把postDate参数转换为 时间戳 进行相等匹配 再返回新数组
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript