Added snippet for getDifferenceInSeconds
This commit is contained in:
33
snippets/covertArrayTo2DMatrix.md
Normal file
33
snippets/covertArrayTo2DMatrix.md
Normal file
@ -0,0 +1,33 @@
|
||||
---
|
||||
title: covertArrayTo2DMatrix
|
||||
tags: array,intermediate
|
||||
---
|
||||
|
||||
Returns array of arrays, with internal arrays of size (columns) supplied.
|
||||
|
||||
```js
|
||||
const covertArrayTo2DMatrix = (items, columns) => {
|
||||
let data = []
|
||||
if (items?.length && columns) {
|
||||
const rows = columns ? Math.ceil((items?.length ?? 0) / columns) : 0
|
||||
for (let i = 0; i < rows; i++) {
|
||||
let row = []
|
||||
for (let j = 0; j < columns; j++) {
|
||||
const element = items[i + j + i * (columns - 1)]
|
||||
if (element) {
|
||||
row.push(element)
|
||||
}
|
||||
}
|
||||
|
||||
data.push(row)
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
covertArrayTo2DMatrix([1, 2, 3, 4, 5, 6], 3); // [[1, 2, 3], [4, 5, 6]]
|
||||
covertArrayTo2DMatrix([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]
|
||||
covertArrayTo2DMatrix([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [4, 5]]
|
||||
```
|
||||
15
snippets/getDifferenceInSeconds.md
Normal file
15
snippets/getDifferenceInSeconds.md
Normal file
@ -0,0 +1,15 @@
|
||||
---
|
||||
title: getDifferenceInSeconds
|
||||
tags: dates,beginner
|
||||
---
|
||||
|
||||
Returns the diffrence in seconds between 2 supplied dates.
|
||||
|
||||
```js
|
||||
const getDifferenceInSeconds = (start, end) =>
|
||||
(end.getTime() - start.getTime()) / 1000;
|
||||
```
|
||||
|
||||
```js
|
||||
getDifferenceInSeconds(new Date('2020-12-24 00:00:15'), new Date('2020-12-24 00:00:17')); // 2
|
||||
```
|
||||
Reference in New Issue
Block a user