Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用forEach()将数组追加到对象中_Javascript_Node.js_Foreach - Fatal编程技术网

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

Javascript 使用forEach()将数组追加到对象中,javascript,node.js,foreach,Javascript,Node.js,Foreach,我正在尝试使用forEach循环和array[key].transactions将对象数组(事务)添加到数组(账单)中。 我也尝试过使用推送,但也没用 来点帮助就好了 var bills = [{ "_id": "5499aece1d7be6c6a3000001", "billName": "Insurance", "startDate": "2014-12-15T00:00:00.000Z", "amount": 2400, "type": 4,

我正在尝试使用forEach循环和array[key].transactions将对象数组(事务)添加到数组(账单)中。 我也尝试过使用推送,但也没用

来点帮助就好了

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

最终结果应该是,测试对象将携带票据和每个票据对象内部的事务作为一个对象数组

编辑 问题是,这段代码在JSFIDLE中有效,但在我应用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}]
谢谢你

像这样的

$.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);
试试这个

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

console.log(bills);
演示:

jQuery版本

演示:

更新:

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对象。(不管怎样,你都在这么做)

使用您的编辑(并且不能够测试它),我认为它应该如下所示:

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

我想表达的一点是IE不支持“forEach”。使用$。每个jquery都应该是选择:)@PrashantKumar IE9在标准模式下是最早支持
forEach
的IE。如果你必须支持IE8,你可以简单地使用polyfill.correct,但如果允许使用,我更喜欢jquery。谢谢你的回答,但我不确定这是一个浏览器的东西,因为我不能让它在FF或Chrome中的本地主机上工作。请参阅编辑。为什么您认为它不起作用?您正在记录交易,不是应该记录账单吗?谢谢@Tomalak的编辑。我尝试了您展示的版本,该版本在JSFIDLE中有效,但在我的本地版本中却不起作用。出于某种原因,测试数组从不携带事务数组。有什么想法吗结果来自mongodb中的两个集合。我的本地代码包含在一个承诺中。all()也许这就是问题所在?请尝试一下我修改过的答案。仍然得到相同的结果。我以前试过。。。无论如何谢谢@Tomalak。不客气。我怀疑原因一定在这里的示例代码之外。恐怕我对Mongoose的了解还不够,无法解释
lean()
,我只能说我的代码需要普通的JavaScript对象,根据文档
lean()
开关似乎是影响它的开关。@Deisy Laymi lean()将MongooseDocument转换为普通的JavaScript对象。。。
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);
    });
});
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);
    });
});