1

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

5 Answers 5

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

1

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],
];

1 Comment

Thanks for your reply, but how can I fix error: Argument of type '(string | number)[][]' is not assignable to parameter of type 'ReadonlyArray<[any, any]>' when building the release?
0

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);

Comments

0

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);

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.