let arrx =
[ 4, [ 1, 2, 3 ], 5, 6 ];
arrx = [].concat.apply([],arr2);
This code above removes the bracket from arrx[1];
so that it becomes [ 4, 1, 2, 3, 5, 6 ]
Can someone please explain how it does this?
let arrx =
[ 4, [ 1, 2, 3 ], 5, 6 ];
arrx = [].concat.apply([],arr2);
This code above removes the bracket from arrx[1];
so that it becomes [ 4, 1, 2, 3, 5, 6 ]
Can someone please explain how it does this?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
what’s x
? what’s arr2
?
because whatever was the value of arrx
, it is overwritten in the line
here arrx
is not mentioned at all on the right, so its previous value doesn’t determine at all the new one
If you can, please write out the entire code. Because we have little to go on here… I mean what is x? or even arr2?
function frankenSplice(arr1, arr2, n) {
let arrx = [...arr2];
arrx.splice(n,0,[...arr1]); //[ 4, [ 1, 2, 3 ], 5, 6 ]
arrx = [].concat.apply([],arrx);
console.log(arrx); // [ 4, 1, 2, 3, 5, 6 ]
return arrx;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
function frankenSplice(arr1, arr2, n) {
let arrx = [...arr2];
arrx.splice(n,0,[...arr1]); //[ 4, [ 1, 2, 3 ], 5, 6 ]
arrx = [].concat.apply([],arrx);
console.log(arrx); // [ 4, 1, 2, 3, 5, 6 ]
return arrx;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
that’s pretty different than what you posted… what question do you have on this code?
function frankenSplice(arr1, arr2, n) {
let arrx = [...arr2];
arrx.splice(n,0,[...arr1]); //[ 4, [ 1, 2, 3 ], 5, 6 ]
arrx = [].concat.apply([],arrx);
console.log(arrx); // [ 4, 1, 2, 3, 5, 6 ]
return arrx;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Sorry, i don’t know this is a solution.
You actually don’t need to have this line in your code. You are adding extra work.
If you just rewrite this line
like this instead
arrx.splice(n,0,...arr1); // notice the lack of brackets around arr1
then you can just return arrx
Your total solution can just be three lines of code.
let arrx = [...arr2];
arrx.splice(n,0,...arr1);
return arrx;
true if not it does not work i think