Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update array push merging two arrays #6913

Merged
merged 3 commits into from
Jul 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,20 @@ <h3 id="Adding_elements_to_an_array">Adding elements to an array</h3>

<h3 id="Merging_two_arrays">Merging two arrays</h3>

<p>This example uses {{jsxref("Function.apply", "apply()")}} to push all elements from a
second array.</p>

<p>Do <em>not</em> use this method if the second array (<code>moreVegs</code> in the
example) is very large because the maximum number of parameters that one function can
take is limited in practice. See {{jsxref("Function.apply", "apply()")}} for more
details.</p>
<p>This example uses {{jsxref("Operators/Spread_syntax", "spread syntax", "", "1")}} to push all elements from a
second array into the first one.</p>

<pre class="brush: js">let vegetables = ['parsnip', 'potato']
let moreVegs = ['celery', 'beetroot']

// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot')
Array.prototype.push.apply(vegetables, moreVegs)
vegetables.push(...moreVegs);

console.log(vegetables) // ['parsnip', 'potato', 'celery', 'beetroot']
</pre>

<p>Merging two arrays can also be done with the {{jsxref("Array.prototype.concat()", "concat()")}} method.</p>

<h3 id="Using_an_object_in_an_array-like_fashion">Using an object in an array-like fashion
</h3>

Expand Down