I am looking to combine 2 arrays in JSON format with keys and values.
MyArray1 [ "Orange:10", "Orange:5", "Banana:20", "Apple:5" ]
MyArray2 [ "Orange:5", "Banana:10", "Apple:15" ]
MyJSON [
{"fruit": "Orange", "value": 15},
{"fruit": "Banana", "value": 20},
{"fruit": "Apple ", "value": 5},
],[
{"fruit": "Orange", "value": 5},
{"fruit": "Banana", "value": 10},
{"fruit": "Apple ", "value": 15},
]
]
I attempted this method but I require key-value pairs to concatenate my 2 arrays together:
MyArray1.forEach(function(val) {
var item = val.split(":");
var key = item[0];
var num = parseInt(item[1], 10);
if (MyArray1.hasOwnProperty(key)) {
MyArray1[key] += num;
} else {
MyArray1[key] = num;
}
});