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

Appending an array into a object with forEach()

我正在尝试使用 forEach 循环和 array[key].transactions 添加对象,将对象数组(交易)添加到数组(bills)中。 我也尝试过使用 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);
    });  


}); 

这个,returns这个:

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

非常感谢!

是这样的吗?

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

试试这个

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

直接修改bill对象即可。 (无论如何你都在这样做)

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