mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 21:44:45 +03:00
Remove note/*
This commit is contained in:
1 parent
2f6e4e4d1d
commit
12201ed08f
288 files changed
+29
-8621
No files matched your search
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,33 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,56 +0,0 @@
|
||||
---
|
||||
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([]);
|
||||
```
|
||||
@@ -1,33 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,38 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,42 +0,0 @@
|
||||
---
|
||||
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' },
|
||||
}
|
||||
*/
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
---
|
||||
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 }
|
||||
```
|
||||
@@ -1,35 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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 }
|
||||
```
|
||||
@@ -1,44 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,40 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,37 +0,0 @@
|
||||
---
|
||||
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]
|
||||
|
||||
*/
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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 = [];
|
||||
```
|
||||
@@ -1,35 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
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 }
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
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);
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
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 }
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
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);
|
||||
```
|
||||
@@ -1,41 +0,0 @@
|
||||
---
|
||||
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']
|
||||
```
|
||||
@@ -1,30 +0,0 @@
|
||||
---
|
||||
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); // []
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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]]
|
||||
```
|
||||
@@ -1,35 +0,0 @@
|
||||
---
|
||||
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'); // []
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
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;
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,30 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
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);
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
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], []);
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,50 +0,0 @@
|
||||
---
|
||||
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' }
|
||||
],
|
||||
}
|
||||
*/
|
||||
```
|
||||
@@ -1,38 +0,0 @@
|
||||
---
|
||||
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])];
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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]]
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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']
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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']
|
||||
```
|
||||
@@ -1,50 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,42 +0,0 @@
|
||||
---
|
||||
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 },
|
||||
// ]
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
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]]
|
||||
```
|
||||
@@ -1,44 +0,0 @@
|
||||
---
|
||||
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],
|
||||
]); // ]
|
||||
```
|
||||
@@ -1,31 +0,0 @@
|
||||
---
|
||||
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]
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
---
|
||||
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
|
||||
*/
|
||||
```
|
||||
@@ -1,37 +0,0 @@
|
||||
---
|
||||
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
|
||||
*/
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
---
|
||||
title: How to add am pm suffix to an hour in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to add am pm suffix to an hour 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 suffixAmPm = h => `${h % 12 === 0 ? 12 : h % 12}${h < 12 ? 'am' : 'pm'}`;
|
||||
```
|
||||
Parameters `h` is an hour number between 0 and 23.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
suffixAmPm(0); // '12am'
|
||||
suffixAmPm(5); // '5am'
|
||||
suffixAmPm(12); // '12pm'
|
||||
suffixAmPm(15); // '3pm'
|
||||
suffixAmPm(23); // '11pm'
|
||||
```
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to calculate the number of difference days between two dates in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the number of difference days between two dates 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 diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
diffDays(new Date('2014-12-19'), new Date('2020-01-01')); // 1839
|
||||
```
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to calculate the number of months between two dates in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the number of months between two dates 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 monthDiff = (startDate, endDate) => Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
monthDiff(new Date('2020-01-01'), new Date('2021-01-01')); // 12
|
||||
```
|
||||
@@ -1,31 +0,0 @@
|
||||
---
|
||||
title: How to compare two dates in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compare two dates 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 compare = (a, b) => a.getTime() > b.getTime();
|
||||
```
|
||||
Parameters `a` and `b` are `Date` instances.
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
compare(new Date('2020-03-30'), new Date('2020-01-01')); // true
|
||||
```
|
||||
@@ -1,32 +0,0 @@
|
||||
---
|
||||
title: How to convert a date to yyyy-mm-dd format in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert a date to yyyy-mm-dd format 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 formatYmd = date => date.toISOString().slice(0, 10);
|
||||
```
|
||||
Parameters `date` is a `Date` object.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
formatYmd(new Date()); // 2020-05-06
|
||||
```
|
||||
@@ -1,45 +0,0 @@
|
||||
---
|
||||
title: How to convert seconds to hh:mm:ss format in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert seconds to hh:mm:ss format 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 formatSeconds = s => new Date(s * 1000).toISOString().substr(11, 8);
|
||||
```
|
||||
Parameters `s` is number of seconds.
|
||||
|
||||
|
||||
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const formatSeconds = s => (new Date(s * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const formatSeconds = s => [parseInt(s / 60 / 60), parseInt(s / 60 % 60), parseInt(s % 60)].join(':').replace(/\b(\d)\b/g, '0$1');
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
formatSeconds(200); // 00:03:20
|
||||
formatSeconds(500); // 00:08:20
|
||||
```
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to extract year, month, day, hour, minute, second and millisecond from a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to extract year, month, day, hour, minute, second and millisecond from a date 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 extract = date => date.toISOString().split(/[^0-9]/).slice(0, -1);
|
||||
```
|
||||
Parameters `date` is a `Date` object.
|
||||
|
||||
|
||||
Parameters `extract` is an array of [year, month, day, hour, minute, second, millisecond].
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
---
|
||||
title: How to format a date for the given locale in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to format a date for the given locale 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 format = (date, locale) => new Intl.DateTimeFormat(locale).format(date);
|
||||
```
|
||||
Parameters `locale` is a locale (en-US, pt-BR, for example).
|
||||
|
||||
|
||||
Parameters `date` is a `Date` object.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
format(new Date(), 'pt-BR'); // 06/05/2020
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to get the current quarter of a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the current quarter of a date 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 getQuarter = (d = new Date()) => Math.ceil((d.getMonth() + 1) / 3);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to get the current timestamp in seconds in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the current timestamp in seconds 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 ts = () => Math.floor(new Date().getTime() / 1000);
|
||||
```
|
||||
@@ -1,32 +0,0 @@
|
||||
---
|
||||
title: How to get the day of the year from a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the day of the year from a date 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 dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24));
|
||||
```
|
||||
Parameters `date` is a Date object.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
dayOfYear(new Date(2020, 04, 16)); // 137
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to get the first date in the month of a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the first date in the month of a date 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 getFirstDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth(), 1);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to get the last date in the month of a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the last date in the month of a date 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 getLastDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth() + 1, 0);
|
||||
```
|
||||
@@ -1,25 +0,0 @@
|
||||
---
|
||||
title: How to get the month name of a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the month name of a date 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 getMonthName = date => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',' November', 'December'][date.getMonth()];
|
||||
```
|
||||
Parameters `date` is a Date object.
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
---
|
||||
title: How to get the number of days in given month in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the number of days in given month 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 daysInMonth = (month, year) => new Date(year, month, 0).getDate();
|
||||
```
|
||||
Parameters `month` is zero-based index.
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to get the timezone string in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the timezone string 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 getTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getTimezone(); // 'Asia/Saigon'
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to get the tomorrow date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the tomorrow date 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 tomorrow = (d => new Date(d.setDate(d.getDate() + 1)))(new Date);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const tomorrow = new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24);
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to get the total number of days in a year in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the total number of days in a year 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 numberOfDays = year => (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) ? 366 : 365;
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const numberOfDays = year => new Date(year, 1, 29).getDate() === 29 ? 366 : 365;
|
||||
```
|
||||
@@ -1,25 +0,0 @@
|
||||
---
|
||||
title: How to get the weekday of a date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the weekday of a date 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 getWeekday = date => ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
|
||||
```
|
||||
Parameters `date` is a Date object.
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to get the yesterday date in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the yesterday date 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 yesterday = (d => new Date(d.setDate(d.getDate() - 1)))(new Date);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const yesterday = new Date((new Date()).valueOf() - 1000 * 60 * 60 * 24);
|
||||
```
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to initialize the current date but set time to midnight in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to initialize the current date but set time to midnight 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 midnightOfToday = () => new Date(new Date().setHours(0, 0, 0, 0));
|
||||
```
|
||||
@@ -1,26 +0,0 @@
|
||||
---
|
||||
title: How to sort an array of dates in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Date Time
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to sort an array of dates 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 sortDescending = arr => arr.sort((a, b) => a.getTime() > b.getTime());
|
||||
const sortAscending = arr => arr.sort((a, b) => a.getTime() < b.getTime());
|
||||
```
|
||||
Parameters `arr` is an array of `Date` items.
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to check if an element is a descendant of another in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if an element is a descendant of another 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 isDescendant = (child, parent) => parent.contains(child);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to check if an element is focused in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if an element is focused 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 hasFocus = ele => (ele === document.activeElement);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to check if the touch events are supported in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if the touch events are supported 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 touchSupported = () => ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to check if user scrolls to the bottom of the page in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if user scrolls to the bottom of the page 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 isAtBottom = () => document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight;
|
||||
```
|
||||
@@ -1,20 +0,0 @@
|
||||
---
|
||||
title: How to detect internet explorer browser in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to detect internet explorer browser only using single line of code in JavaScript.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isIE = !!document.documentMode;
|
||||
```
|
||||
@@ -1,20 +0,0 @@
|
||||
---
|
||||
title: How to detect macos browser in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to detect macos browser only using single line of code in JavaScript.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isMacBrowser = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to get all siblings of an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get all siblings of an element 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 siblings = ele => [].slice.call(ele.parentNode.children).filter((child) => (child !== ele));
|
||||
```
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to get the position of an element relative to the document in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the position of an element relative to the document 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 getPosition = ele => (r = ele.getBoundingClientRect(), { left: r.left + window.scrollX, top: r.top + window.scrollY });
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getPosition(document.body); // { left: 0, top: 0 }
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to get the selected text in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the selected text 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 getSelectedText = () => window.getSelection().toString();
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to go back to the previous page in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to go back to the previous page 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}
|
||||
history.back();
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
history.go(-1);
|
||||
```
|
||||
@@ -1,35 +0,0 @@
|
||||
---
|
||||
title: How to hide an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to hide an element 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}
|
||||
// Pick the method that is suitable for your use case
|
||||
const hide = ele => ele.style.display = 'none';
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const hide = ele => ele.style.visibility = 'hidden';
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const hide = ele => ele.hidden = true;
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to insert an element after other one in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to insert an element after other one 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 insertAfter = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle.nextSibling);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const insertAfter = (ele, anotherEle) => anotherEle.insertAdjacentElement('afterend', ele);
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to insert an element before other one in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to insert an element before other one 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 insertBefore = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const insertBefore = (ele, anotherEle) => anotherEle.insertAdjacentElement('beforebegin', ele);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to insert given html after an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to insert given html after an element 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 insertHtmlAfter = (html, ele) => ele.insertAdjacentHTML('afterend', html);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to insert given html before an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to insert given html before an element 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 insertHtmlBefore = (html, ele) => ele.insertAdjacentHTML('beforebegin', html);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to redirect to another page in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to redirect to another page 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 goTo = url => location.href = url;
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to reload the current page in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to reload the current page 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 reload = () => location.reload();
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const reload = () => (location.href = location.href);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to replace an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to replace an element 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 replace = (ele, newEle) => ele.parentNode.replaceChild(newEle, ele);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to scroll to top of the page in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to scroll to top of the page 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 goToTop = () => window.scrollTo(0, 0);
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to serialize form data in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to serialize form data 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 serialize = formEle => Array.from(new FormData(formEle)).reduce((p, [k, v]) => Object.assign({}, p, { [k]: p[k] ? (Array.isArray(p[k]) ? p[k] : [p[k]]).concat(v) : v}), {});
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to show an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to show an element 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 show = ele => ele.style.display = '';
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
---
|
||||
title: How to strip html from a given text in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to strip html from a given text 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 stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';
|
||||
```
|
||||
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to toggle an element in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- DOM
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to toggle an element 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 toggle = ele => (ele.style.display = (ele.style.display === 'none') ? 'block' : 'none');
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const toggle = ele => ele.hidden = !ele.hidden;
|
||||
```
|
||||
@@ -1,38 +0,0 @@
|
||||
---
|
||||
title: How to box handler in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to box handler 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 boxHandler = x => ({ next: f => boxHandler(f(x)), done: f => f(x) });
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const getPercentNumber = str => boxHandler(str).next(str => str.replace(/\%/, '')).next(str => parseFloat(str)).done(res => res * 0.01);
|
||||
|
||||
getPercentNumber('50%'); // 0.5
|
||||
|
||||
// Example 2
|
||||
const getMoney = (price) => Number.parseFloat(price.replace(/\$/, ''));
|
||||
const getPercent = (percent) => Number.parseFloat(percent.replace(/\%/)) * 0.01;
|
||||
|
||||
const getDiscountPrice = (price, discount) => boxHandler(getMoney(price)).done(cents => boxHandler(getPercent(discount)).next(save => cents - (cents * save))).done(res => res);
|
||||
|
||||
getDiscountPrice('$6.00', '20%'); // 4.8
|
||||
```
|
||||
@@ -1,30 +0,0 @@
|
||||
---
|
||||
title: How to check if a value is a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if a value is a function 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 isFunction = v => ['[object Function]', '[object GeneratorFunction]', '[object AsyncFunction]', '[object Promise]'].includes(Object.prototype.toString.call(v));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isFunction(function() {}); // true
|
||||
isFunction(function*() {}); // true
|
||||
isFunction(async function() {}); // true
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
---
|
||||
title: How to check if a value is a generator function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if a value is a generator function 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 isGeneratorFunction = v => Object.prototype.toString.call(v) === '[object GeneratorFunction]';
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isGeneratorFunction(function() {}); // false
|
||||
isGeneratorFunction(function*() {}); // true
|
||||
```
|
||||
@@ -1,30 +0,0 @@
|
||||
---
|
||||
title: How to check if a value is an async function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if a value is an async function 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 isAsyncFunction = v => Object.prototype.toString.call(v) === '[object AsyncFunction]';
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isAsyncFunction(function() {}); // false
|
||||
isAsyncFunction(function*() {}); // false
|
||||
isAsyncFunction(async function() {}); // true
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
---
|
||||
title: How to compose functions from left to right in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compose functions from left to right 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}
|
||||
// Compose functions from left to right
|
||||
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const lowercase = str => str.toLowerCase();
|
||||
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
|
||||
const reverse = str => str.split('').reverse().join('');
|
||||
|
||||
const fn = pipe(lowercase, capitalize, reverse);
|
||||
|
||||
// We will execute `lowercase`, `capitalize` and `reverse` in order
|
||||
fn('Hello World') === 'dlrow olleH';
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
---
|
||||
title: How to compose functions in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compose functions 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}
|
||||
// Compose functions from right to left
|
||||
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const lowercase = str => str.toLowerCase();
|
||||
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
|
||||
const reverse = str => str.split('').reverse().join('');
|
||||
|
||||
const fn = compose(reverse, capitalize, lowercase);
|
||||
|
||||
// We will execute `lowercase`, `capitalize` and `reverse` in order
|
||||
fn('Hello World') === 'dlrow olleH';
|
||||
```
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
---
|
||||
title: How to create a function that accepts a single argument in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to create a function that accepts a single argument 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 unary = fn => arg => fn(arg);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
['1', '2', '3', '4', '5'].map(unary(parseInt)); // [1, 2, 3, 4, 5]
|
||||
```
|
||||
Loaded 100 of 288 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user