How does js add up the amountMoney in the salary array where postDate is the same month to return a new array?

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

]
}

Mar.03,2021

array.map ()


mainly because I don't know how to add amountMoney with the same number of months, that is, the content of map ().


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; iPP) {
    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; jPP) {
            if (rs[j].month == month) {
                rs[j].amountMoney += Number(salary[i].amountMoney)
            }
        }
    }
}
console.log(rs);

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"
}])

I don't know if it meets your needs. Because it is merged by month, the returned postDate shows the month. The result is as follows:

clipboard.png


convert the postDate parameter to a timestamp for equal matching and return a new array

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eaee9b-2211.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eaee9b-2211.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?