af8462edb7f584a9d12248e1c1dec63758de0606
[xogo.git] / utils / array.js
1 export const ArrayFun = {
2
3 // Double array intialization
4 init: function(size1, size2, initElem) {
5 return [...Array(size1)].map(() => Array(size2).fill(initElem));
6 },
7
8 range: function(min, max) {
9 if (!max) {
10 max = min;
11 min = 0;
12 }
13 return [...Array(max - min).keys()].map(k => k + min);
14 },
15
16 toObject: function(keys, values) {
17 if (!Array.isArray(values))
18 // Second argument is a scalar
19 values = Array(keys.length).fill(values);
20 return (
21 ArrayFun.range(keys.length)
22 .reduce((acc, curr) => (acc[keys[curr]] = values[curr], acc), {})
23 );
24 }
25
26 };