Javascript: insert array in another array at a specific index [duplicate] - javascript

This question already has answers here:
Javascript - insert an array inside another array
(11 answers)
Closed 5 years ago.
I have two arrays:
a = [1,2,3]
b = [4,5,6]
I'd like to insert b at index 1 of a, to have :
c = [1,4,5,6,2,3]
Is there a builtin function to do this ?
I found the answer for a single element, but not for a whole array.
I imagine something like concat but with an additional parameter which would be the index of insertion.

Use Array#splice method.
a = [1, 2, 3]
b = [4, 5, 6]
// copy array a
c = a.slice();
// provide array of arguments using apply method
// and insert elements using splice method
[].splice.apply(c, [1, 0].concat(b))
console.log(c);

var a = [1,2,3],
b = [4,5,6];
a.splice(1, 0, ...b);
console.log(a);

Related

Why "let a = [1, 2, 3]; console.log(a === [1, 2, 3]);" is "false" in JavaScript? [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 4 years ago.
Why
let a = [1, 2, 3];
console.log(a === [1, 2, 3]);
is "false" in JavaScript?
Javascript Objects are a bit like C pointers.
a contains the memory address of the first array you define.
When you do console.log(a === [1, 2, 3]); you are in fact creating a new array, and you compare its memory value with the one you kept in a.
That's why:
const a = [];
a.push(1)
is valid (the constant is the "pointer", not the array)
Because the arrays aren't the same. The variable a contains a different array than the one in the console.log even though the array values are the same. You would have to loop through both arrays and compare each value of both to determine if they were equal.

how can i have the first 3 elements of an array of variable length in Javascript [duplicate]

This question already has answers here:
How to get first N number of elements from an array
(14 answers)
Closed 5 years ago.
i would like to get the first 3 elements of an array of variable length. i've sorted my array and i would like to get a Top 3.
here's what i've done :
var diffSplice = this.users.length - 1;
return this.users.sort(this.triDec).splice(0,diffSplice)
my "solution" work only for an array of 4 element ( -1 )
Is there a better way to use the splice method ?
Thanks for your help
You could use Array#slice for the first three items.
return this.users.sort(this.triDec).slice(0, 3);
Don't you want to use a const value for diffSplice like
var diffSplice = 3;
return this.users.sort(this.triDec).slice(0,diffSplice)
try running
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(0, 3));
refer to Array Silce
Fill out the deletecount for Splice:
var sortedArray = this.users.sort(this.triDec);
return sortedArray.splice(0, 3);
check MDN

Javascript array explication [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 6 years ago.
Given this two arrays a and b:
var a = [1,2,3];
var b = a;
a.push(4);
console.log(b); /* [1,2,3,4] */
console.log(a); /* [1,2,3,4] */
Why isn't b equal with [1,2,3] ?
The variable b holds the reference to array a. You need to copy the array instead use Array#slice method to copy.
var a = [1, 2, 3];
var b = a.slice();
a.push(4);
console.log(b);
console.log(a);

.splice() - how to protect oryginal array? [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 7 years ago.
var a = [1, 2, 3];
var b = a; // b = [1, 2, 3];
console.log(a); // 1, 2, 3
console.log(b); // 1, 2, 3
b.splice(0, 1);
console.log(b); // 2, 3
console.log(a); // 2, 3 <----------- WHY?
I just needed to copy my oryginal "a" array because I want to stay it as [1, 2, 3] forever. How to pop first element from "b" array without touching the oryginal one? Thanks
Your code just needs one small fix:
var a = [1, 2, 3];
var b = a.slice();
I'm not sure of the specifics, but when you are assigning arrays or objects to another variable, the array/object is not copied by value, but rather by reference. .slice method duplicates all elements in the array to the new element rather than just saving a reference to the old one.

Array.prototype.push.apply unexpected behaviour

I am trying to use Array.prototype.push.apply to merge two lists.
c = Array.prototype.push.apply(a, b);
However, this does not merge the arrays when the second one is [].
for instance if
a = ['x', 'y', 'z']
b = []
c will be 3
Why is this happening?
Shouldn't [] be treated like any array?
Just use Array.prototype.concat:
c = a.concat(b);
It is perfectly correct, because Array.push() will return the length of the new array.
If you want a new array which has the concatenated value then use Array.concat() instead.
What you may have been trying to achieve is using push.apply to append b to a. However this method means that you don't have to create a new array c to hold the result.
var a = [1, 2, 3, 4], b = [5];
a.push.apply(a, b); // a = [1, 2, 3, 4, 5]
Your real problem is the .apply, it ask the contetx (a) and an array of values (b), if you pass an empty array it acts like you have passed no values...
Try this:
c = Array.prototype.push.call(a, b);
//c = 4

Categories