3 回答
TA贡献1820条经验 获得超10个赞
您可以尝试这样做,方法是创建一个对象作为映射,然后使用它来获取输出数组。使用Javascript MAP存储结果
var vale_data = [
{service: "allocateorder", vol_slo: "10000"},
{service: "cancelorder", vol_slo: "10000"},
{service: "adviceorder", vol_slo: "10000"}]
var sli_data = [
{service: "allocateorder", vol_sli: "0.9484949"},
{service: "cancelorder", vol_sli: "0.242322"}]
var sli_dataMap = new Map(sli_data.map(({service,vol_sli}) => ([service,vol_sli])));
vale_data = vale_data.map(obj => ({...obj,vol_sli:sli_dataMap.get(obj.service) || ''}));
console.log(vale_data);
TA贡献1836条经验 获得超4个赞
var vale_data = [
{service: "allocateorder", vol_slo: "10000"},
{service: "cancelorder", vol_slo: "10000"},
{service: "adviceorder", vol_slo: "10000"}];
var sli_data = [
{service: "allocateorder", vol_sli: "0.9484949"},
{service: "cancelorder", vol_sli: "0.242322"}];
vale_data.map(vale_record => {
let rec_found = sli_data.find(sli_record => sli_record.service === vale_record.service);
if(rec_found) {
vale_record['vol_sli'] = rec_found['vol_sli'];
}
else {
vale_record['vol_sli'] = ''
}
return vale_record;
});
console.log(vale_data);
TA贡献1833条经验 获得超4个赞
实现这一点的可能方法之一是使用map和find以下方式:
var vale_data = [{
service: "allocateorder",
vol_slo: "10000"
},
{
service: "cancelorder",
vol_slo: "10000"
},
{
service: "adviceorder",
vol_slo: "10000"
}
];
var sli_data = [{
service: "allocateorder",
vol_sli: "0.9484949"
},
{
service: "cancelorder",
vol_sli: "0.242322"
}
];
var result = vale_data.map(element => {
let sli = sli_data.find(service => service["service"] === element["service"]) ||
{ vol_sli: "" };
return { ...element,
...sli
};
});
console.log(result);
添加回答
举报