Home > Software design >  Insert an array into another array
Insert an array into another array

Time:11-03

I am handling a Javascript function to have a behaviour below:

1/ When the checkbox button is checked, an array of setting strings is inserted into another array

var RB_array_1=[];
var mycheckboxButton1 = document.getElementById('mycheckboxButton1');
if (mycheckboxButton.checked===true) {
  RB_array_1=["setting A_1","setting B_1"]
}

2/ It will insert into another array while it remain the array inside previously:

const final_array = 
  [ RB_array_1
  , RB_array_2
  , ...
  , RB_array_x
  ] 

and it should remain array inside the array:

const final_array = 
  [ [ 'setting A_1', 'setting B_1'] 
  , [ 'setting A_2', 'setting B_2'] 
  , ...
  , [ 'setting A_x', 'setting B_x'] 
  ]      

Is there any way to insert an array into another array ?

CodePudding user response:

Seems like you're looking for the push method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

const arr1 = [1,2,3];
const arr2 = [4,5,6];
const arrs = [arr1];

arrs.push(arr2);

console.log(arrs);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

RB_array_1.push(["setting A_1","setting B_1"])

just make sure whatever you push inside your final array element also has to be an ARRAY. Should work fine according to me.

Refer this for an example

  • Related