-1

i tried to push an array to other array in specific order with this javascript code :

var arr1=["A","B","C","D","E","F"];
var arr2=["1","2","3"]

console.log(arr1.splice(0,-1,arr2));

its reutn [];

my desire rusult : ["1","2","3","A","B","C","D","F"]

please any body show me how to achieve my desire result with splice function ps : i can achieve this with loop

Thx

EDIT: sorry, My question was misleading. This is my actual condition:

arr1 :[["A","B","C"],["D","E","F"]]
arr2 :["1","2","3"]

Expected output : [["1,"2","3","A","B","C"],["1","2","3","D","E","F"]]

I have tried:

arr1.map(function(e) {
    return e.splice(0, -1, arra2)
});

but I got: [],[]

adiga
  • 34,372
  • 9
  • 61
  • 83
Rio Arfani
  • 89
  • 9

5 Answers5

2

You can use Spread syntax like this const result = [...arr2, ...arr1];

Code:

const arr1 = ["A","B","C","D","E","F"];
const arr2 = ["1","2","3"]
const result = [...arr2, ...arr1];

console.log(result);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1

Since other solutions creates a new array as result, we could use an approach by modifying in-place your original array by using unshift method in combination with spread syntax.

var arr1=["A","B","C","D","E","F"];
var arr2=["1","2","3"]

arr1.unshift(...arr2);
console.log(arr1);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

Do you have to use splice? The concat methods does exactly what you want to achieve.

var arr1=["A","B","C","D","E","F"];
var arr2=["1","2","3"]

var result = arr2.concat(arr1);
console.log(result);

// If you're okay with using spread syntax (doesn't work in IE), you could just:
console.log([...arr2, ...arr1]);
JLe
  • 2,835
  • 3
  • 17
  • 30
  • uisng concate, can be done if arra1 and array2 in same dimension, but in my case arr1 is 2D and arr1 1D, so i can't change the order – Rio Arfani Nov 30 '18 at 10:14
  • @RioArfani Can you update your example so that arr1 is 2D as you want it to be? – JLe Nov 30 '18 at 10:15
0

Spliche works for the given array and if you like to add the values of arr2 at the beginning of the array, then you could splice the array with a spreaded array.

Array#splice returns the array which ts taken out of the given array by the count of the second parameter, which id zero here, and not -1 whcih makes no sense.

var arr1 = ["A", "B", "C", "D", "E", "F"],
    arr2 = ["1", "2", "3"]

arr1.splice(0, 0, ...arr2); // returns []

console.log(arr1);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

From docs, return value of Array.splice is

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

It will not return the updated array and as you are not removing any element, it will return an empty array.

To add some entries using splice at a particular index, you can try following.

var arr1=["A","B","C","D","E","F"];
var arr2=["1","2","3"]
arr1.splice(0,0, ...arr2);
console.log(arr1);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59