2015-01-04 84 views
1

我試圖使用forEach循環和數組[鍵] .transactions添加對象添加對象(事務)數組到數組(票據)。 我也嘗試使用推,但沒有工作。使用forEach()添加一個數組到一個對象()

一些幫助將是可愛的。

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但是當我應用到陣列從貓鼬發現()正在添加不起作用。 下面是完整的代碼:

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

我是歡迎您!

回答

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

@Deisy Laymi lean()將MongooseDocument轉換爲純JavaScript對象... http://goo.gl/kxicLp –

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/

+0

有一件事我要傳達是「forEach」不支持IE。使用$。每個jquery應該是選擇:) –

+1

有點像更新? – Trace

+0

@PrashantKumar標準模式下的IE9是最早支持'forEach'的IE。如果你必須*支持IE8,你可以簡單地使用一個polyfill。 – Tomalak

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

感謝編輯@Tomalak。我試過你顯示的版本,它在jsfiddle中有效,但不知何故它不在我的本地版本中。測試數組由於某種原因從不攜帶事務數組。任何想法? - 結果來自MongoDB中的兩個集合。 –

+0

我的本地代碼是在promise.all()裏面的,也許這就是問題所在? –

+0

請給我修改過的答案一下。 – Tomalak

相關問題