I have 2 Arrays,
arr1 = [
['itemid-1', 'itemclass', 'timestamp'],
['itemid-2', 'itemclass', 'timestamp'],
['itemid-3', 'itemclass', 'timestamp'],
['itemid-5', 'itemclass', 'timestamp']
];
arr2 = [
['itemid-1', 'data-state', 'data-col'],
['itemid-3', 'data-state', 'data-col'],
['itemid-4', 'data-state', 'data-col']
];
The end result should be:
arr1 = [
['itemid-1', 'itemclass', 'timestamp', 'data-state', 'data-col'],
['itemid-2', 'itemclass', 'timestamp'],
['itemid-3', 'itemclass', 'timestamp', 'data-state', 'data-col'],
['itemid-5', 'itemclass', 'timestamp']
];
I want to merge values from arr2
to arr1
where itemid-x
are same.
I can do this by using loops,
for(i = 0; i < arr1.length; i++){
for(j = 0; j < arr2.length; j++){
if(arr1[i][0] == arr2[j][0]){
arr1[i] = arr1[i].concat(arr2[j].slice(1));
}
}
}
however I have recently started with underscorejs and nodejs, so I would like to know if it can be done with any existing functionality.
PS: I have found this answer interesting, but it requires the length of the arrays to be same, also it won't work if the itemid-x
indices are not same in both arrays.