Im trying to add the array of objects(transactions) into the the array(bills) using forEach to loop and array[key].transactions to add the objects. I have also tried to use push but that didn't work neither.
Some help would be lovely.
var bills = [{
"_id": "5499aece1d7be6c6a3000001",
"billName": "Insurance",
"startDate": "2014-12-15T00:00:00.000Z",
"amount": 2400,
"type": 4,
"timestamp": "2014-12-23T18:05:02.987Z",
"__v": 0
}, {
"_id": "549bf0597886c3763e000001",
"billName": "Leasing",
"startDate": "2014-12-25T00:00:00.000Z",
"endDate": "2017-10-14T22:00:00.000Z",
"amount": 16500,
"type": 4,
"timestamp": "2014-12-25T11:09:13.957Z",
"__v": 0
}];
var transactions = [{
"_id": "549ea8c957b654ef64000003",
"billId": "5499aece1d7be6c6a3000001",
"paymentDate": "2014-12-27T12:40:41.311Z",
"amount": 2400,
"timestamp": "2014-12-27T12:40:41.311Z",
"__v": 0
}, {
"_id": "549ea8e632ed3f6265000001",
"billId": "549bf0597886c3763e000001",
"paymentDate": "2014-12-27T12:41:10.582Z",
"amount": 16500,
"timestamp": "2014-12-27T12:41:10.582Z",
"__v": 0
}, {
"_id": "549ea93452ebbd8366000001",
"billId": "549bf0597886c3763e000001",
"paymentDate": "2014-12-27T12:42:28.744Z",
"amount": 16500,
"timestamp": "2014-12-27T12:42:28.745Z",
"__v": 0
}];
and
var test = [];
bills.forEach(function (bill, key) {
test.push(bill);
transactions.forEach(function (transaction) {
if (transaction.billId == bill._id) test[key].transactions = transaction;
});
});
console.log(test);
The end result should be the test object would carry the bills plus the transaction inside each bill objects as an object array.
Muchas gracias!
Aucun commentaire:
Enregistrer un commentaire