Deisy Laymi
Deisy Laymi

Reputation: 343

Appending an array into a object with forEach()

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.

EDIT

The problem is that this code works in JSfiddle but doesn't work when I apply to the array comming from the mongoose find(). Here is the complete code:

    app.get('/getbills', function(req,res) {

    //get all bills
    allBills =  Bills.find().exec();

    //get all transactions
    allTransactions = Transactions.find().exec();

    //aggregate bills and transactions in a promise
    promise.all([allBills, allTransactions]).then(function(results){

        var bills = results[0];
        var transactions = results[1];
        var test = [];
       // var test.transactions = [];

        bills.forEach(function(bill, key) {
            bill.transactions = transactions.filter(function (transaction) {
               return transaction.billId === bill._id;
        });
            console.log(bill.transactions);
            test.push(bill);
        });

        console.log(transactions);
        res.send(test);
    });  


}); 

This, returns this:

[{"_id":"5499aece1d7be6c6a3000001","billName":"Jeep 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},{"_id":"54a014bfac01ca3526000001","billName":"Test","startDate":"2014-10-28T00:00:00.000Z","endDate":"2017-12-19T23:00:00.000Z","amount":1000,"type":4,"timestamp":"2014-12-28T14:33:35.233Z","__v":0}]

Muchas gracias!

Upvotes: 1

Views: 810

Answers (3)

Oleksandr T.
Oleksandr T.

Reputation: 77482

Try this

bills.forEach(function(bill, key) {
  bill.transactions = transactions.filter(function (el) {
    return el.billId === bill._id;
  });
});

console.log(bills);

Demo: http://jsbin.com/bocasi/5/edit?js,console

jQuery version

$.each(bills, function(i, bill) {
  bill.transactions = $.grep(transactions, function (el) {
    return el.billId === bill._id;
  });
});

console.log(bills);

Demo: http://jsbin.com/bocasi/4/edit?js,console

Update:

app.get('/getbills', function(req, res) {

    var allBills        = Bills.find().lean().exec();
    var allTransactions = Transactions.find().lean().exec();

    promise.all([allBills, allTransactions]).then(function(results) {

        var bills        = results[0];
        var transactions = results[1];
        var test         = [];

        bills.forEach(function(bill, key) {
            bill.transactions = transactions.filter(function(transaction) {
                return transaction.billId === bill._id;
            });
        });

        res.send(bills);
    });
});

Upvotes: 2

Tomalak
Tomalak

Reputation: 338316

Simply modify the bill objects directly. (You're doing that anyway)

bills.forEach(function (bill) {
    bill.transactions = transactions.filter(function (transaction) {
        return transaction.billId === bill._id;
    });
});

With your edit (and without being able to test it), I think it should look like this:

app.get('/getbills', function (req, res) {
    var allBills = Bills.find().exec();
    var allTransactions = Transactions.find().exec();

    promise.all([allBills, allTransactions]).then(function (results) {
        var bills = results[0];
        var transactions = results[1];

        bills.forEach(function (bill) {
            bill.transactions = transactions.filter(function (transaction) {
                return transaction.billId === bill._id;
            });
        });
        res.send(bills);
    });
});

Upvotes: 1

Trace
Trace

Reputation: 18889

Something like this?

$.each(bills, function(index, bill){ 
    var arr_temp = []; 
    $.each(transactions, function(index, transaction){
        if(bill._id == transaction.billId){
            arr_temp.push(transaction); 
        }
    }); 
    bill.transactions = arr_temp; 
}); 

 console.log(bills);

http://jsfiddle.net/8c7a89s2/1/

Upvotes: 0

Related Questions