A
A
Alexey Shchepetkin2019-12-09 08:33:29
JavaScript
Alexey Shchepetkin, 2019-12-09 08:33:29

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

1 answer(s)
A
Alexey Ukolov, 2019-12-09
@schepetkin

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]

Why the variable is needed is calso not very clear - arrays are assigned by reference, so it awill change too.
In general, the strange code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question