How to compare two array objects and append first array object on match criteria key with second ? Pls suggest.
For example, in the below data, I need to compare vale_data service key with sli_data service key and if it matches, then append sli_data vol_sli key value to 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"}]
I have tried like below, but it didn't worked.
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'] })
}
}
}
Expected output:
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: ""} ]
Actual output:
error: Uncaught TypeError: Cannot read property 'service' of undefined