jika Anda sudah memiliki dua objek maka Anda dapat melakukan seperti ini. tetapi jika Anda perlu mendapatkannya dari mongoDb maka Anda harus menggunakan $mergeObject dalam agregasi mongo.
var a = [
{
"_id" : "5ba8d8dfaa988532967029af",
"level" : 2,
"completed" : 5,
"asset" : "5ba8caa1aa98853296702989"
},
{
"_id" : "5ba8d8dfaa988532967029b0",
"level" : 2,
"completed" : 3,
"asset" : "5ba8caf6aa9885329670298a"
}];
var b = [
{
"total" : 1,
"asset" : "5ba8caa1aa98853296702989",
"level" : 2
},
{
"total" : 1,
"asset" : "5ba8caf6aa9885329670298a",
"level" : 2
}];
var output = [];
function extend(obj, src) {
src.forEach(function(key,index) {
if(obj[index]["asset"] == src[index]["asset"] && obj[index]["level"] == src[index]["level"]){
let c = {...obj[index],...src[index]};
output.push(c);
}
});
}
extend(a, b);
console.log(output)