I have the array: [[1,2,3], [1,2,2], [4,3]]. Then I want to add the array: [3,3,3]. The result should be [[1,2,3], [1,2,2], [4,3], [3,3,3]]
My code:
const arr1 = [[1,2,3], [1,2,2], [4,3]];
const addArr = [3,3,3];
const result = [].concat(arr1 , addArr );
console.log(result);
The log is: [[1,2,3], [1,2,2], [4,3], 3, 3, 3] Why?
Thanks
[].concat(arr1 , [addArr] )works alsoarr1.concat([addArr])!push(addArr), any changes toaddArrwill change arr1. So, make sure you usepush(addArr.slice())to push a copy, not just a reference.