如何比较两个数组对象并在匹配条件键上附加第一个数组对象和第二个?请建议。
例如,在下面的数据中,我需要将vale_data服务密钥与sli_data服务密钥进行比较,如果匹配,则将sli_datavol_sli 密钥值附加到vale_data.
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 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"}]
for (i=0; i<Object.keys(vale_data).length; i++) {
for (j=0; j<Object.keys(sli_data).length; j++) {
if (Object.values(vale_data)[i]['service'] == Object.values(sli_data)[i]['service']) {
vale_data.map(item => {item.vol_sli = Object.values(sli_data)[j]['vol_sli'] })
}
}
}
预期输出:
vale_data = [
{service: "allocateorder", vol_slo: "10000", vol_sli: "0.9484949"},
{service: "cancelorder", vol_slo: "10000", vol_sli: "0.242322"},
{service: "adviceorder", vol_slo: "10000", vol_sli: ""} ]
实际输出:
error: Uncaught TypeError: Cannot read property 'service' of undefined
拉莫斯之舞
HUH函数
潇潇雨雨
相关分类