'Is there a way to update a readonly array of objects

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



Solution 1:[1]

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]

Solution 2:[2]

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]

mergedArray will be a new array that we create by merging both arr1 and arr2. Then we check for unique elements in mergedArray by using the forEach method and add non-repeating elements to finalArray.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2