Hello,
Is it possible to use spread operator (…) with splice() ?
Exemple:
If I want add an arr1 in other arr2 from his index “n”:
let result = arr2.splice(n, 0, ...arr1);
Is it possible or not ?
Hello,
Is it possible to use spread operator (…) with splice() ?
Exemple:
If I want add an arr1 in other arr2 from his index “n”:
let result = arr2.splice(n, 0, ...arr1);
Is it possible or not ?
Seems like it might be possible. What happens if you try it?
They don’t talk about … (spread).
furthermore they give as example only arrays with strings inside.
But that’s works now.
This my solution:
function frankenSplice(arr1, arr2, n) {
let copyArr2 = arr2.slice(0);
copyArr2.splice(n, 0, ...arr1);
return copyArr2;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
I made the same mistake like in this other topic.
https://forum.freecodecamp.org/t/title-case-a-sentence-cascading-methodes/476760/3
I’m still really not well understand with that.
If I write this code, that not works but why ?
function frankenSplice(arr1, arr2, n) {
let copyArr2 = arr2.slice(0);
let result = copyArr2.splice(n, 0, ...arr1);
return result;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Hiiii actually I think I just understood
An array is an array. There is nothing special about arrays of strings vs arrays of numbers.
Spread syntax (
...
) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) … are expected
So I would expect spread to work fine in turning an array into a number of optional arguments for the splice()
function call.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.