我的代码
var result = [];
var datax = [{
"tag": "TH000144"
}, {
"tag": "TH000147"
}, {
"tag": "TH000166"
}, {
"tag": "TH000169"
}, {
"tag": "TH000170"
}];
var newdatax = [{
"serial": "14563",
"tag": "TH000144"
}, {
"serial": "7777",
"tag": "TH000147"
}, {
"tag": "TH000169",
"serial": null
}];
var result = lodash.merge(datax, newdatax);
结果
[{
"tag": "TH000144",
"serial": "14563"
}, {
"tag": "TH000147",
"serial": "7777"
}, {
"tag": "TH000169",
"serial": null
}, {
"tag": "TH000169"
}, {
"tag": "TH000170"
}]
我需要这样
[{
"tag": "TH000144",
"serial": "14563"
}, {
"tag": "TH000147",
"serial": "7777"
}, {
"tag": "TH000169",
"serial": null
}, {
"tag": "TH000166"
}, {
"tag": "TH000170"
}]
我的代码错在哪里?
答案 0 :(得分:0)
反转合并的参数,例如_.merge(newdatax, datax);
我对出了什么问题的看法:
当递归合并函数到达TH000169
时,它解析为未定义;似乎很可能是由于null
值的串行。文档提到了这种潜在的挂起HERE。文档还建议采用_.merge(destination, source);
形式。
var result = [];
var datax = [{
"tag": "TH000144"
}, {
"tag": "TH000147"
}, {
"tag": "TH000166"
}, {
"tag": "TH000169"
}, {
"tag": "TH000170"
}];
var newdatax = [{
"serial": "14563",
"tag": "TH000144"
}, {
"serial": "7777",
"tag": "TH000147"
}, {
"tag": "TH000169",
"serial": null
}];
var result = _.merge(newdatax, datax);
console.log(JSON.stringify(result, null, 4));
结果
[
{
"serial": "14563",
"tag": "TH000144"
},
{
"serial": "7777",
"tag": "TH000147"
},
{
"tag": "TH000166",
"serial": null
},
{
"tag": "TH000169"
},
{
"tag": "TH000170"
}
]