-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: [],[]

3
  • 1
    do you want a new variable or an update? Commented Nov 30, 2018 at 10:09
  • Where is E gone from the output? :p Commented Nov 30, 2018 at 10:15
  • Even with your edit Rio it's still a duplicate, the question is the same just using map as well Commented Nov 30, 2018 at 10:34

5 Answers 5

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);

Sign up to request clarification or add additional context in comments.

1 Comment

I love this answer :)
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);

Comments

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]);

2 Comments

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
@RioArfani Can you update your example so that arr1 is 2D as you want it to be?
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);

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.