Add JS array merge blog
This commit is contained in:
@ -9,9 +9,9 @@ excerpt: JavaScript arrays have a very robust API offering a plethora of amazing
|
||||
|
||||
JavaScript arrays have a very robust API offering a plethora of amazing tools. Here are our top 4 JavaScript array methods every developer should know:
|
||||
|
||||
**[`Array.prototype.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)**
|
||||
**Array.prototype.map()**
|
||||
|
||||
`Array.prototype.map()` creates a new array by applying the provided transformation to each element of the original array. The result is an array with the same length as the original array and elements transformed based on the provided function.
|
||||
[`Array.prototype.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) creates a new array by applying the provided transformation to each element of the original array. The result is an array with the same length as the original array and elements transformed based on the provided function.
|
||||
|
||||
```js
|
||||
const arr = [1, 2, 3];
|
||||
@ -19,9 +19,9 @@ const double = x => x * 2;
|
||||
arr.map(double); // [2, 4, 6]
|
||||
```
|
||||
|
||||
**[`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)**
|
||||
**Array.prototype.filter()**
|
||||
|
||||
`Array.prototype.filter()` creates a new array by using a filtering function to keep only elements that return `true` based on that function. The result is an array with equal or less than the original array's length, containing a subset of the same elements as the original array.
|
||||
[`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) creates a new array by using a filtering function to keep only elements that return `true` based on that function. The result is an array with equal or less than the original array's length, containing a subset of the same elements as the original array.
|
||||
|
||||
```js
|
||||
const arr = [1, 2, 3];
|
||||
@ -31,9 +31,9 @@ arr.filter(isOdd); // [1, 3]
|
||||
|
||||

|
||||
|
||||
**[`Array.prototype.reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)**
|
||||
**Array.prototype.reduce()**
|
||||
|
||||
`Array.prototype.reduce()` creates an output value of any type depending on a reducer function and an initial value. The result can be of any type such as an integer, an object or an array, based on the reducer function provided.
|
||||
[`Array.prototype.reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) creates an output value of any type depending on a reducer function and an initial value. The result can be of any type such as an integer, an object or an array, based on the reducer function provided.
|
||||
|
||||
```js
|
||||
const arr = [1, 2, 3];
|
||||
@ -45,9 +45,9 @@ const increment = (x, y) => [...x, x[x.length - 1] + y];
|
||||
arr.reduce(increment, [0]); // [0, 1, 3, 6]
|
||||
```
|
||||
|
||||
**[`Array.prototype.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)**
|
||||
**Array.prototype.find()**
|
||||
|
||||
`Array.prototype.find()` returns the first element for which a matcher function returns `true`. The result is a single element from the original array.
|
||||
[`Array.prototype.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) returns the first element for which a matcher function returns `true`. The result is a single element from the original array.
|
||||
|
||||
```js
|
||||
const arr = [1, 2, 3];
|
||||
|
||||
57
blog_posts/javascript-merge-arrays.md
Normal file
57
blog_posts/javascript-merge-arrays.md
Normal file
@ -0,0 +1,57 @@
|
||||
---
|
||||
title: How do I merge two arrays in JavaScript?
|
||||
type: question
|
||||
tags: javascript,array
|
||||
authors: chalarangelo
|
||||
cover: blog_images/arrays.jpg
|
||||
excerpt: Arrays are one of the most used data types in any programming language. Learn how to merge two arrays in JavaScript with this short guide.
|
||||
---
|
||||
|
||||
**Spread operator**
|
||||
|
||||
The [spread operator (`...`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) was introduced in ES6 and can be used to merge two or more arrays, by spreading each one inside a new array:
|
||||
|
||||
```js
|
||||
const a = [1, 2, 3];
|
||||
const b = [4, 5, 6];
|
||||
|
||||
const merged = [...a, ...b]; // [1, 2, 3, 4, 5, 6]
|
||||
```
|
||||
|
||||
**Array.prototype.concat()**
|
||||
|
||||
[`Array.prototype.concat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) is a method on the `Array` prototype and can be used to create a new array, either by concatenating both arrays to a new array or one array to the other. Both methods result in a new array, without mutating the original:
|
||||
|
||||
```js
|
||||
const a = [1, 2, 3];
|
||||
const b = [4, 5, 6];
|
||||
|
||||
const merged = [].concat(a, b); // [1, 2, 3, 4, 5, 6]
|
||||
// -- OR --
|
||||
const alsoMerged = a.concat(b); // [1, 2, 3, 4, 5, 6]
|
||||
```
|
||||
|
||||
**Comparing the two**
|
||||
|
||||
The spread operator version is definitely shorter and as readable as the `Array.prototype.concat()` one. Apart from that, the spread operator seems to be slightly faster based on [some benchmarks I have performed](https://jsben.ch/9txyg) (as of **Aug, 2020 on Google Chrome 84** - this might or might not be the case in the future, as new optimizations land in different browsers).
|
||||
|
||||
However, `Array.prototype.concat()` can deal with non-array values better than the spread operator can, which might be something to consider when merging values that you are not certain are arrays:
|
||||
|
||||
```js
|
||||
const a = [1, 2, 3];
|
||||
const b = true;
|
||||
const c = 'hi';
|
||||
|
||||
const spreadAb = [...a, ...b]; // Error: b is not iterable
|
||||
const spreadAc = [...a, ...c]; // [1, 2, 3, 'h', 'i'], wrong result
|
||||
// You should use [...a, b] and [...a, c] instead
|
||||
|
||||
const concatAb = [].concat(a, b); // [1, 2, 3, true]
|
||||
const concatAb = [].concat(a, c); // [1, 2, 3, 'hi']
|
||||
```
|
||||
|
||||
As you can see in the above example, the spread operator either throws an error or doesn't output the correct result when passed a non-iterable object. `Array.prototype.concat()` on the other hand has no trouble being passed mixed input.
|
||||
|
||||
So what's the verdict? Use the spread operator (`...`) whenever you know your inputs are arrays, as it performs better and is easy to read and understand. Favor `Array.prototype.concat()` when you are uncertain of one or more of the inputs and do not want to add additional checks, as it handles those cases more gracefully.
|
||||
|
||||
**Image credit:** [Edgar Chaparro](https://unsplash.com/@echaparro) on [Unsplash](https://unsplash.com/s/photos/code?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
|
||||
Reference in New Issue
Block a user