Home > Mobile >  How to merge two subarrays in JavaScript without changing the order of the array
How to merge two subarrays in JavaScript without changing the order of the array

Time:04-08

I need to merge two subarrays inside an array without pushing them to the end of the array

let array = [["a"], ["b"], ["c"], ["d"]];
array[1] = array[1].concat[2];

So I basically want array to be

array = [["a"], ["bc"], ["d"]];

Thanks!

CodePudding user response:

You could splice the array and remove the item for adding to another item.

let array = [["a"], ["b"], ["c"], ["d"]];

array[1][0]  = array.splice(2, 1).join('');

console.log(array);

CodePudding user response:

const arr = [['a'], ['b'], ['c'], ['d']];

arr[1][0]  = arr[2][0];
arr.splice(2, 1);
console.log(arr);
  • Related