Home > database >  Is there a way to update a readonly array of objects
Is there a way to update a readonly array of objects

Time:04-22

I do have two arrays

arr1 = [1,2,3,4] //readonly array
arr2 = [1,2,3,4,5,6,7]

arr1 is the main array and I want to update it with contents from arr2 so that when i log arr1 it will display

[1,2,3,4,5,6,7]

is there a way I can do this

CodePudding user response:

let arr1 = [1,2,3,4];
let arr2 = [1,2,3,4,5,6,7];
arr1 = [...new Set([...arr1 ,...arr2])];

console.log(arr1); // [1,2,3,4,5,6,7]

CodePudding user response:

var mergedArray = new Array(...arr1, arr2) // [1,2,3,4,1,2,3,4,5,6,7]
var finalArray = []

mergedArray.forEach(item => {
    if (!finalArray.includes(element)) { 
        finalArray.push(element);
    }
})

console.log(finalArray) // [1,2,3,4,5,6,7]
  • Related