On my server, I have a paymentList JSON that includes date and time. Utilizing moment.js, I am attempting to create a new property called paymentTime to store the time data, but it seems to not update as expected.
this.paymentList.forEach(element => {
console.log(moment(element.paymentDate).format("hh:mm A"));
console.log(moment(element.paymentDate).format("DD-MMMM-YYYY"));
element.paymentTime = moment(element.paymentDate).format("hh:mm A");
element.paymentDate = moment(element.paymentDate).format("DD-MMMM-YYYY");
console.log(this.paymentList);
});
[
{
"paymentDate": "2018-01-09T09:21:13.759Z",
"loanAmount": "918553377499",
},
{
"paymentDate": "2017-12-27T09:30:41.276Z",
"loanAmount": "9880712423",
},
{
"paymentDate": "2017-12-27T07:49:09.892Z",
"loanAmount": "7259945267",
}
]
I acknowledge that changing my code structure will resolve the issue, however, I am seeking an elegant solution.
this.paymentList.forEach(element => {
var array = [];
var obj = {paymentTime: '', paymentDate: '', loanAmount: ''}
obj.paymentDate= moment(element.paymentDate).format("DD-MMMM-YYYY");
obj.paymentTime = moment(element.paymentDate).format("hh:mm A");
obj.loanAmount = element.loanAmount;
array.push(obj);
this.paymentList = array;
});