Reputation: 1591
My initial array is like this:
preData = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
The array with values is like this:
preData = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
I would like to update the values of first array from the second one, the result:
finalData= [
['APP_NOT_RUNNING', 2],
['FALLBACK', 0],
['IDLE', 3],
['OUTOFSERVICE', 0]
];
I would appreciate any help
Upvotes: 1
Views: 662
Reputation: 37918
One of the possible solutions would be converting initial array to Map
, updating it and converting it back to array:
const preData = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
const valueData = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
const map = new Map(preData);
for (const [key, value] of valueData) {
map.set(key, value);
}
const result = Array.from(map);
console.log(result);
Update - typings could be:
const preData: ReadonlyArray<[string, number]> = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
const valueData: Array<[string, number]> = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
Upvotes: 1
Reputation: 7742
We can use .map
& .find
preData = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
preData2 = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
const finalData = preData.map(x=>{
const found = preData2.find(y=>y[0]===x[0])
if(found)
x[1] = found[1]
return x;
})
console.log(finalData);
Upvotes: 0
Reputation: 1655
You will need to iterate over the preData array and check if a value exists for each value in the actualData array or not. If it doesnt, just push the default valye to the finalData array and if it does, add the value and then push it.
use forEach to iterate over the preData array and use filter to check if the data exists for each value in preData.
const preData = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
const actualData = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
const finalData = [];
preData.forEach(preDataValue => {
const index = finalData.push(preDataValue);
const data = actualData.filter(d => d[0] === preDataValue[0]);
if (data.length) {
finalData[index-1][1] += data[0][1];
}
});
console.log(finalData);
Upvotes: 0
Reputation: 39322
You can use Map()
and .forEach()
method of arrays:
let arr1 = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
let arr2 = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
let updateArray = (a1, a2, map = new Map()) => {
a1.forEach(arr => map.set(arr[0], arr));
a2.forEach(arr => map.set(arr[0], arr));
return [...map.values()];
};
console.log(updateArray(arr1, arr2));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2
Reputation: 16908
Match the the second array actualData
content of index '0' with the original array preData
index '0'. If it is a match, copy over that content from index '1' (the value) of actualData
into the original array.
var preData = [
['APP_NOT_RUNNING', 0],
['FALLBACK', 0],
['IDLE', 0],
['OUTOFSERVICE', 0]
];
var actualData = [
['APP_NOT_RUNNING', 2],
['IDLE', 3],
];
preData.forEach((ele, idx, arr) =>{
actualData.forEach(item =>{
if(item[0] === ele[0]){
ele[1] = item[1];
arr[idx] = ele;
}
});
});
console.log(preData);
Upvotes: 0