Is it possible to insert an array inside another keeping its original array form? I looked on google and didn't find what I wanted, so I'll try here. Should look like this:
arr1 = [1, 2 , 3]
arr2 = [4, 5, 6]
console.log(arr3) // [[1, 2, 3], [4, 5, 6]]
CodePudding user response:
Create a new array that references your other arrays...
const arr3 = [arr1, arr2];
console.log(arr3);
// [[1, 2, 3], [4, 5, 6]]
CodePudding user response:
Or this one use array.push()
let arr1 = [1, 2 , 3]
let arr2 = [4, 5, 6]
let arr3= []
arr3.push(arr1,arr2)
console.log(arr3)
CodePudding user response:
Yes its possible like the below way
var arr1 = new Array (1, 2, 3);
var arr2 = new Array (4, 5, 6);
var arr3 = new Array();
arr3[0] = arr1;
arr3[1] = arr2;
console.log(JSON.stringify(arr3))
CodePudding user response:
Yes, absolutely, it's possible:
const arr1 = [1, 2 , 3]
const arr2 = [4, 5, 6]
const arr3 = []
arr3.push(arr1)
arr3.push(arr2)
console.log(arr3)
The new array holds the references to arr1 and arr2. Even if the references to arr1 and arr2 become unaccessible, the content of these array will not be garbage collected if arr3 is accessible.
