0

I am doing the Netflix tutorial on Reactive Programming and I came across this piece of code that I don't fully understand.

Array.prototype.mergeAll = function() {
    var results = [];

    this.forEach(function(subArray) {
        results.push.apply(results, subArray);
    });

    return results;
};

Why use apply in this line of code results.push.apply(results, subArray);? Why not just use results.push(subArray)? What is the difference in the code?

3
  • Looks like someone is not familiar with concatreturn Array.prototype.concat.apply([], this) Commented Sep 2, 2014 at 19:35
  • Wouldn't that make an array of arrays, and not just one big array? So you would end up with exactly what you started with ? Commented Sep 2, 2014 at 19:59
  • No. concat takes multiple arrays as arguments - and that's just what we give it with apply. Commented Sep 3, 2014 at 9:56

2 Answers 2

4

The results wouldn't be the same at all. You can test this for yourself:

results = [1, 2, 3];
subArray = [4, 5, 6];

results.push(subArray);

console.log("With push:", results); // With push: [1, 2, 3, [4, 5, 6]]


results = [1, 2, 3]; // reset results

results.push.apply(results, subArray);

console.log("With apply:", results); // With apply: [1, 2, 3, 4, 5, 6]

apply accepts an array of arguments, and the elements of the array become individual arguments to the function.

This lets you invoke the function push with an arbitrary number of arguments, where each argument is added to the array. Simply calling results.push(...) would invoke the function with a single argument, which would be an array, resulting in the entire subArray being pushed onto results as one element.

In the above example, results.push.apply(results, subArray) is equivalent to calling results.push(4, 5, 6), while results.push(subArray) simply invokes results.push([4, 5, 6]).

The net effect of using one over the other is that, given an input array containing sub-arrays...

[[1, 2, 3], [4, 5, 6], [7, 8], [9]]

... using results.push would produce an identical array, where each sub-array was pushed onto results as a single element:

[[1, 2, 3], [4, 5, 6], [7, 8], [9]]

Using results.push.apply would cause each element from each sub-array to be pushed onto results as its own element, resulting in the desired flattened array:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sign up to request clarification or add additional context in comments.

Comments

0

Example 1:

var results = [];

results.push.apply(results,[2,3,4])

results [2,3,4]

Example 2:

var results2 = [];

[].push.apply(results2,[2,3,4])

// results2 [2,3,4]

results.push.apply is identical to [].push.apply. Both of them represent array's push method.

the first parameter in apply() method : results / results2, representing the scope/this inside Array.prototype.push method.

Here is the link giving a good example, how the first parameter in apply() method works.

If you want to know, why the second parameter of apply() method is array of arguments. This link's example has good explanation.

Basically

[].push.apply(results2,[2,3,4])

// results2 [2, 3, 4]


result2.push(2,3,4)

// results2 [2, 3, 4]

The first method is equal to the second method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.