Answer the question
In order to leave comments, you need to log in
Incomprehensible record of merging two arrays in js. How does this happen?
Good afternoon colleagues! I saw a variant of merging two arrays, but I can't understand how it works. I know how the apply method works , but in this expression it is not clear, and it is not clear why we first assign a reference to the array a , then a comma and push through apply.
Can someone explain in a nutshell how it works? Thank you very much in advance!!
var a = [1,2,3,4],
b = [3,4,5,6],
с;
c = a, Array.prototype.push.apply(a,b) // [1, 2, 3, 4, 3, 4, 5, 6]
Answer the question
In order to leave comments, you need to log in
If it is the comma that causes difficulties for you, then this is just a crooked record, it is not clear why.
This code is similar to yours, but much clearer:
var a = [1,2,3,4];
var b = [3,4,5,6];
var с;
c = a;
Array.prototype.push.apply(a,b) // [1, 2, 3, 4, 3, 4, 5, 6]
c
also not very clear - arrays are assigned by reference, so it a
will change too. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question