使用forEach()将数组附加到对象中

时间:2015-01-04 16:42:14

标签: javascript node.js foreach

我试图使用forEach to循环和数组[key] .transactions将对象数组(事务)添加到数组(bill)中以添加对象。 我也尝试过使用push,但这两种方法都没有用。

一些帮助会很可爱。

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
}];

 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);

最终结果应该是测试对象将账单加上每个账单对象内的交易作为对象数组。

修改

问题是这个代码在JSfiddle中有效,但是当我应用于来自mongoose find()的数组时,它不起作用。 这是完整的代码:

    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);
    });  


}); 

这个,返回:

[{"_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!

3 个答案:

答案 0 :(得分:2)

试试这个

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

console.log(bills);

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

jQuery版

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

console.log(bills);

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

更新:

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);
    });
});

答案 1 :(得分:1)

只需直接修改帐单对象即可。 (无论如何你都是这样做的)

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

通过编辑(并且无法测试),我认为它应该如下所示:

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);
    });
});

答案 2 :(得分:0)

这样的东西?

$.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/