Skip to content

Commit f4249aa

Browse files
committed
add common_js/2darray_to_collection
Signed-off-by: Alex Ivanov <ai@contributor.pw>
1 parent 95766bc commit f4249aa

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"timeZone": "Europe/Moscow",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8"
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "standalone"
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Converts a 2D array to a collection.
3+
*
4+
* @param {Array.<Array.<any>>} array
5+
* @returns {Array.<object>}
6+
*/
7+
function arrayToCollection_(array) {
8+
return array.slice(1).map(
9+
(_, rowIndex) =>
10+
array[0].reduce((rowCollection, header, columnIndex) => {
11+
rowCollection[header] = array[rowIndex + 1][columnIndex];
12+
return rowCollection;
13+
}, {}),
14+
[]
15+
);
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function arrtocoll_(e) {
2+
return e
3+
.slice(1)
4+
.map((r, c) => e[0].reduce((r, n, o) => ((r[n] = e[c + 1][o]), r), {}), []);
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: 'Converts a 2D array to a collection'
3+
date: '2021-07-13'
4+
description: 'Simple conversion of an array into a collection is a common task. There is not always a desire to use wrappers or proxies.'
5+
tags: ['common']
6+
categories: ['snippets']
7+
images: ['./snippets/common_js/2darray_to_collection/screenshot.png']
8+
---
9+
10+
## Simple conversion of an array into a collection
11+
12+
{{< toc >}}
13+
14+
![Simple conversion of an array into a collection](./screenshot.png)
15+
16+
### Snippet
17+
18+
- {{< externalLink >}}
19+
- {{< commentLink >}}
20+
- {{< scrvizLink >}}
21+
22+
{{< codeFromFile "index.js" >}}
23+
24+
### Run it
25+
26+
{{< codeFromFile "run.js" >}}
27+
28+
### Minimized version
29+
30+
For quick use, you can use a more transparent version
31+
32+
{{< codeFromFile "index.min.js" >}}
33+
34+
{{< clipboard >}}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* global arrayToCollection_ arrtocoll_ */
2+
3+
const ARRAY = [
4+
['Num', 'Date', 'Name', 'Amount'],
5+
[1, new Date(), 'Alex', 100],
6+
[2, new Date(), 'Nate', 200],
7+
[3, new Date(), 'Piter', 300],
8+
];
9+
10+
/**
11+
* Runs the snippet
12+
*/
13+
function run() {
14+
// Don't forget a deep copy
15+
const array = ARRAY.map((r) => [...r]);
16+
// Headings become keys
17+
array[0].forEach((_, i) => (array[0][i] = array[0][i].toLocaleLowerCase()));
18+
const collection = arrayToCollection_(array);
19+
console.log(collection);
20+
}
21+
22+
/**
23+
* Runs the minimized function of the snippet
24+
*/
25+
function runMiniFn() {
26+
// Don't forget a deep copy
27+
const array = ARRAY.map((r) => [...r]);
28+
// Headings become keys
29+
array[0].forEach((_, i) => (array[0][i] = array[0][i].toLocaleLowerCase()));
30+
const collection = arrtocoll_(array);
31+
console.log(collection);
32+
}
56.8 KB
Loading

0 commit comments

Comments
 (0)