mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
note
This commit is contained in:
1 parent
7fc7945881
commit
098238ad13
286 files changed
+8596
No files matched your search
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to cast a value as an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to cast a value as an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const castArray = value => Array.isArray(value) ? value : [value];
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
castArray(1); // [1]
|
||||
castArray([1, 2, 3]); // [1, 2, 3]
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to check if an array is empty in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if an array is empty only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isEmpty = arr => !Array.isArray(arr) || arr.length === 0;
|
||||
```
|
||||
Parameters `arr` is an array.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isEmpty([]); // true
|
||||
isEmpty([1, 2, 3]); // false
|
||||
```
|
||||
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: How to clone an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to clone an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const clone = arr => arr.slice(0);
|
||||
```
|
||||
Parameters `arr` is an array.
|
||||
|
||||
|
||||
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const clone = arr => [...arr];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const clone = arr => Array.from(arr);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const clone = arr => arr.map(x => x);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const clone = arr => JSON.parse(JSON.stringify(arr));
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const clone = arr => arr.concat([]);
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to compare two arrays regardless of order in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compare two arrays regardless of order only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
|
||||
```
|
||||
Parameters `a` and `b` are arrays.
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isEqual([1, 2, 3], [1, 2, 3]); // true
|
||||
isEqual([1, 2, 3], [1, 3, 2]); // true
|
||||
isEqual([1, 2, 3], [1, '2', 3]); // false
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: How to compare two arrays in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compare two arrays only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
||||
```
|
||||
Parameters `a` and `b` are arrays.
|
||||
|
||||
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const isEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isEqual([1, 2, 3], [1, 2, 3]); // true
|
||||
isEqual([1, 2, 3], [1, '2', 3]); // false
|
||||
```
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: How to convert an array of objects to a single object in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert an array of objects to a single object only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
toObject(
|
||||
[
|
||||
{ id: '1', name: 'Alpha', gender: 'Male' },
|
||||
{ id: '2', name: 'Bravo', gender: 'Male' },
|
||||
{ id: '3', name: 'Charlie', gender: 'Female' },
|
||||
],
|
||||
'id'
|
||||
);
|
||||
/*
|
||||
{
|
||||
'1': { id: '1', name: 'Alpha', gender: 'Male' },
|
||||
'2': { id: '2', name: 'Bravo', gender: 'Male' },
|
||||
'3': { id: '3', name: 'Charlie', gender: 'Female' },
|
||||
}
|
||||
*/
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to convert an array of strings to numbers in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert an array of strings to numbers only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const toNumbers = arr => arr.map(Number);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const toNumbers = arr => arr.map(x => +x);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
toNumbers(['2', '3', '4']); // [2, 3, 4]
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: How to count by the properties of an array of objects in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to count by the properties of an array of objects only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const countBy = (arr, prop) => arr.reduce((prev, curr) => (prev[curr[prop]] = ++prev[curr[prop]] || 1, prev), {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
countBy([
|
||||
{ branch: 'audi', model: 'q8', year: '2019' },
|
||||
{ branch: 'audi', model: 'rs7', year: '2020' },
|
||||
{ branch: 'ford', model: 'mustang', year: '2019' },
|
||||
{ branch: 'ford', model: 'explorer', year: '2020' },
|
||||
{ branch: 'bmw', model: 'x7', year: '2020' },
|
||||
], 'branch');
|
||||
|
||||
// { 'audi': 2, 'ford': 2, 'bmw': 1 }
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: How to count the occurrences of a value in an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to count the occurrences of a value in an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const countOccurrences = (arr, val) => arr.filter(item => item === val).length;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
countOccurrences([2, 1, 3, 3, 2, 3], 2); // 2
|
||||
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a'); // 3
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to count the occurrences of array elements in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to count the occurrences of array elements only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const countOccurrences = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
countOccurrences([2, 1, 3, 3, 2, 3]); // { '1': 1, '2': 2, '3': 3 }
|
||||
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b']); // { 'a': 3, 'b': 2, 'c': 1 }
|
||||
```
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
title: How to create an array of cumulative sum in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to create an array of cumulative sum only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const accumulate = arr => arr.map((sum => value => sum += value)(0));
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const accumulate = arr => arr.reduce((a, b, i) => i === 0 ? [b] : [...a, b + a[i - 1]], []);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const accumulate = arr => arr.reduce((a, b, i) => i === 0 ? [b] : [...a, b + a[i - 1]], 0);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
accumulate([1, 2, 3, 4]); // [1, 3, 6, 10]
|
||||
// 1 = 1
|
||||
// 1 + 2 = 3
|
||||
// 1 + 2 + 3 = 6
|
||||
// 1 + 2 + 3 + 4 = 10
|
||||
```
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: How to create an array of numbers in the given range in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to create an array of numbers in the given range only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const range = (min, max) => [...Array(max - min + 1).keys()].map(i => i + min);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const range = (min, max) => Array(max - min + 1).fill(0).map((_, i) => min + i);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const range = (min, max) => Array.from({ length: max - min + 1 }, (_, i) => min + i);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
range(5, 10); // [5, 6, 7, 8, 9, 10]
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
title: How to create cartesian product in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to create cartesian product only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const cartesian = (...sets) => sets.reduce((acc, set) => acc.flatMap((x) => set.map((y) => [...x, y])), [[]]);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
cartesian([1, 2], [3, 4]); // [ [1, 3], [1, 4], [2, 3], [2, 4] ]
|
||||
|
||||
/*
|
||||
3 4
|
||||
---------------
|
||||
1 | [1, 3] [1, 4]
|
||||
|
|
||||
2 | [2, 3] [2, 4]
|
||||
|
||||
*/
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to empty an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to empty an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const empty = arr => arr.length = 0;
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
arr = [];
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: How to find the closest number from an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the closest number from an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// Find the number from `arr` which is closest to `n`
|
||||
const closest = (arr, n) => arr.reduce((prev, curr) => Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const closest = (arr, n) => arr.sort((a, b) => Math.abs(a - n) - Math.abs(b - n))[0];
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50); // 33
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: How to find the index of the last matching item of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the index of the last matching item of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const lastIndex = (arr, predicate) => arr.reduce((prev, curr, index) => predicate(curr) ? index : prev, -1);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const lastIndex = (arr, predicate) => arr.map(item => predicate(item)).lastIndexOf(true);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
lastIndex([1, 3, 5, 7, 9, 2, 4, 6, 8], i => i % 2 === 1); // 4
|
||||
lastIndex([1, 3, 5, 7, 9, 8, 6, 4, 2], i => i > 6); // 5
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to find the index of the maximum item of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the index of the maximum item of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const indexOfMax = arr => arr.reduce((prev, curr, i, a) => curr > a[prev] ? i : prev, 0);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
indexOfMax([1, 3, 9, 7, 5]); // 2
|
||||
indexOfMax([1, 3, 7, 7, 5]); // 2
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to find the index of the minimum item of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the index of the minimum item of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const indexOfMin = arr => arr.reduce((prev, curr, i, a) => curr < a[prev] ? i : prev, 0);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
indexOfMin([6, 4, 8, 2, 10]); // 3
|
||||
indexOfMin([6, 4, 2, 2, 10]); // 2
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to find the length of the longest string in an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the length of the longest string in an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const findLongest = words => Math.max(...(words.map(el => el.length)));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
findLongest(['always','look','on','the','bright','side','of','life']); // 6
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to find the maximum item of an array by given key in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the maximum item of an array by given key only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const maxBy = (arr, key) => arr.reduce((a, b) => a[key] >= b[key] ? a : b, {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const people = [
|
||||
{ name: 'Bar', age: 24 },
|
||||
{ name: 'Baz', age: 32 },
|
||||
{ name: 'Foo', age: 42 },
|
||||
{ name: 'Fuzz', age: 36 },
|
||||
];
|
||||
maxBy(people, 'age'); // { name: 'Foo', age: 42 }
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: How to find the maximum item of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the maximum item of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const max = arr => Math.max(...arr);
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to find the minimum item of an array by given key in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the minimum item of an array by given key only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const minBy = (arr, key) => arr.reduce((a, b) => a[key] < b[key] ? a : b, {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const people = [
|
||||
{ name: 'Bar', age: 24 },
|
||||
{ name: 'Baz', age: 32 },
|
||||
{ name: 'Foo', age: 42 },
|
||||
{ name: 'Fuzz', age: 36 },
|
||||
];
|
||||
minBy(people, 'age'); // { name: 'Bar', age: 24 }
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: How to find the minimum item of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to find the minimum item of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const min = arr => Math.min(...arr);
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
title: How to flatten an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to flatten an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a));
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const flat = arr => arr.reduce((a, b) => Array.isArray(b) ? [...a, ...flat(b)] : [...a, b], []);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
// See the browser compatibility at https://caniuse.com/#feat=array-flat
|
||||
const flat = arr => arr.flat();
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
flat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to get all arrays of consecutive elements in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get all arrays of consecutive elements only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getConsecutiveArrays = (arr, size) => size > arr.length ? [] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getConsecutiveArrays([1, 2, 3, 4, 5], 2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
|
||||
getConsecutiveArrays([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
|
||||
getConsecutiveArrays([1, 2, 3, 4, 5], 6); // []
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to get all n-th items of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get all n-th items of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getNthItems = (arr, nth) => arr.filter((_, i) => i % nth === nth - 1);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getNthItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 2); // [2, 4, 6, 8]
|
||||
getNthItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); // [3, 6, 9]
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to get all subsets of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get all subsets of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getSubsets([1, 2]); // [[], [1], [2], [1, 2]]
|
||||
getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: How to get indices of a value in an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get indices of a value in an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const indices = (arr, value) => arr.reduce((acc, v, i) => (v === value ? [...acc, i] : acc), []);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const indices = (arr, value) => arr.map((v, i) => v === value ? i : false).filter(Boolean);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
indices(['h', 'e', 'l', 'l', 'o'], 'l'); // [2, 3]
|
||||
indices(['h', 'e', 'l', 'l', 'o'], 'w'); // []
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: How to get the average of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the average of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to get the intersection of arrays in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the intersection of arrays only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getIntersection = (a, ...arr) => [...new Set(a)].filter(v => arr.every(b => b.includes(v)));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getIntersection([1, 2, 3], [2, 3, 4, 5]); // [2, 3]
|
||||
getIntersection([1, 2, 3], [2, 3, 4, 5], [1, 3, 5]); // [3]
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to get the rank of an array of numbers in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the rank of an array of numbers only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const ranking = (arr) => arr.map((x, y, z) => z.filter(w => w > x).length + 1);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
ranking([80, 65, 90, 50]); // [2, 3, 1, 4]
|
||||
ranking([80, 80, 70, 50]); // [1, 1, 3, 4]
|
||||
ranking([80, 80, 80, 50]); // [1, 1, 1, 4]
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: How to get the sum of an array of numbers in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the sum of an array of numbers only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const sum = arr => arr.reduce((a, b) => a + b, 0);
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to get the unique values of an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the unique values of an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const unique = arr => [...new Set(arr)];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const unique = arr => arr.filter((el, i, array) => array.indexOf(el) === i);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const unique = arr => arr.reduce((acc, el) => acc.includes(el) ? acc : [...acc, el], []);
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to get union of arrays in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get union of arrays only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const union = (...arr) => [...new Set(arr.flat())];
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
union([1, 2], [2, 3], [3]); // [1, 2, 3]
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: How to group an array of objects by a key in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to group an array of objects by a key only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const groupBy = (arr, key) => arr.reduce((acc, item) => ((acc[item[key]] = [...(acc[item[key]] || []), item]), acc), {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
groupBy([
|
||||
{ branch: 'audi', model: 'q8', year: '2019' },
|
||||
{ branch: 'audi', model: 'rs7', year: '2020' },
|
||||
{ branch: 'ford', model: 'mustang', year: '2019' },
|
||||
{ branch: 'ford', model: 'explorer', year: '2020' },
|
||||
{ branch: 'bmw', model: 'x7', year: '2020' },
|
||||
], 'branch');
|
||||
|
||||
/*
|
||||
{
|
||||
audi: [
|
||||
{ branch: 'audi', model: 'q8', year: '2019' },
|
||||
{ branch: 'audi', model: 'rs7', year: '2020' }
|
||||
],
|
||||
bmw: [
|
||||
{ branch: 'bmw', model: 'x7', year: '2020' }
|
||||
],
|
||||
ford: [
|
||||
{ branch: 'ford', model: 'mustang', year: '2019' },
|
||||
{ branch: 'ford', model: 'explorer', year: '2020' }
|
||||
],
|
||||
}
|
||||
*/
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: How to merge two arrays in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to merge two arrays only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// Merge but don't remove the duplications
|
||||
const merge = (a, b) => a.concat(b);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const merge = (a, b) => [...a, ...b];
|
||||
|
||||
// Merge and remove the duplications
|
||||
const merge = [...new Set(a.concat(b))];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const merge = [...new Set([...a, ...b])];
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to partition an array based on a condition in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to partition an array based on a condition only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const partition = (arr, criteria) => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
partition([1, 2, 3, 4, 5], n => n % 2); // [[2, 4], [1, 3, 5]]
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to remove duplicate values in an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to remove duplicate values in an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const removeDuplicate = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
removeDuplicate(['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']); // ['h', 'e', 'w', 'r', 'd']
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to remove falsy values from array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to remove falsy values from array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const removeFalsy = arr => arr.filter(Boolean);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
removeFalsy([0, 'a string', '', NaN, true, 5, undefined, 'another string', false]); // ['a string', true, 5, 'another string']
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: How to repeat an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to repeat an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const repeat = (arr, n) => [].concat(...Array(n).fill(arr));
|
||||
```
|
||||
Parameters `arr` is an array.
|
||||
|
||||
|
||||
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const repeat = (arr, n) => Array(n).fill(arr).flat();
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const repeat = (arr, n) => Array(arr.length * n).fill(0).map((_, i) => arr[i % arr.length]);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const repeat = (arr, n) => Array.from({ length: arr.length * n }, (_, i) => arr[i % arr.length]);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
repeat([1, 2, 3], 3); // [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to shuffle an array in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to shuffle an array only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const shuffle = arr => arr.map(a => ({ sort: Math.random(), value: a })).sort((a, b) => a.sort - b.sort).map(a => a.value);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const shuffle = arr => arr.sort(() => .5 - Math.random());
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // [9, 1, 10, 6, 8, 5, 2, 3, 7, 4]
|
||||
```
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: How to sort an array of items by given key in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to sort an array of items by given key only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const sortBy = (arr, k) => arr.concat().sort((a, b) => (a[k] > b[k]) ? 1 : ((a[k] < b[k]) ? -1 : 0));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const people = [
|
||||
{ name: 'Foo', age: 42 },
|
||||
{ name: 'Bar', age: 24 },
|
||||
{ name: 'Fuzz', age: 36 },
|
||||
{ name: 'Baz', age: 32 },
|
||||
];
|
||||
sortBy(people, 'age');
|
||||
|
||||
// returns
|
||||
// [
|
||||
// { name: 'Bar', age: 24 },
|
||||
// { name: 'Baz', age: 32 },
|
||||
// { name: 'Fuzz', age: 36 },
|
||||
// { name: 'Foo', age: 42 },
|
||||
// ]
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to sort an array of numbers in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to sort an array of numbers only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const sort = arr => arr.sort((a, b) => a - b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
sort([1, 5, 2, 4, 3]); // [1, 2, 3, 4, 5]
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to split an array into chunks in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to split an array into chunks only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3); // [[1, 2, 3], [4, 5, 6], [7, 8]]
|
||||
chunk([1, 2, 3, 4, 5, 6, 7, 8], 4); // [[1, 2, 3, 4], [5, 6, 7, 8]]
|
||||
```
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
title: How to swap the rows and columns of a matrix in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to swap the rows and columns of a matrix only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const transpose = matrix => matrix[0].map((col, i) => matrix.map(row => row[i]));
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const transpose = matrix => matrix[0].map((col, c) => matrix.map((row, r) => matrix[r][c]));
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const transpose = matrix => matrix.reduce((prev, next) => next.map((item, i) => (prev[i] || []).concat(next[i])), []);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
transpose([ // [
|
||||
[1, 2, 3], // [1, 4, 7],
|
||||
[4, 5, 6], // [2, 5, 8],
|
||||
[7, 8, 9], // [3, 6, 9],
|
||||
]); // ]
|
||||
```
|
||||
@@ -0,0 +1,31 @@
|
||||
---
|
||||
title: How to swap two array items in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to swap two array items only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const swapItems = (a, i, j) => a[i] && a[j] && [...a.slice(0, i), a[j], ...a.slice(i + 1, j), a[i], ...a.slice(j + 1)] || a;
|
||||
```
|
||||
Parameters `i` must be less than `j`.
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
swapItems([1, 2, 3, 4, 5], 1, 4); // [1, 5, 3, 4, 2]
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: How to unzip an array of arrays in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to unzip an array of arrays only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const unzip = arr => arr.reduce((acc, c) => (c.forEach((v, i) => acc[i].push(v)), acc), Array.from({ length: Math.max(...arr.map(a => a.length)) }, (_) => []));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
unzip([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]]); // [['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]]
|
||||
|
||||
/*
|
||||
a 1
|
||||
b 2
|
||||
c 3
|
||||
d 4
|
||||
e 5
|
||||
*/
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
title: How to zip multiple arrays in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Array
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to zip multiple arrays only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const zip = (...arr) => Array.from({ length: Math.max(...arr.map(a => a.length)) }, (_, i) => arr.map(a => a[i]));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
zip(['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]); // [['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]]
|
||||
|
||||
/*
|
||||
Does it look like a zipper?
|
||||
a 1
|
||||
b 2
|
||||
c 3
|
||||
d 4
|
||||
e 5
|
||||
*/
|
||||
```
|
||||
Reference in new issue
Block a user