Started code review + some fixes (unfinished)
[vchess.git] / client / src / utils / array.js
1 // Remove item(s) in array (if present)
2 export const ArrayFun = {
3 remove: function(arr, rfun, all) {
4 const index = arr.findIndex(rfun);
5 if (index >= 0) {
6 arr.splice(index, 1);
7 if (all) {
8 // Reverse loop because of the splice below
9 for (let i = arr.length - 1; i >= index; i--) {
10 if (rfun(arr[i])) arr.splice(i, 1);
11 }
12 }
13 }
14 },
15
16 // Double array intialization
17 init: function(size1, size2, initElem) {
18 return [...Array(size1)].map(() => Array(size2).fill(initElem));
19 },
20
21 range: function(max) {
22 return [...Array(max).keys()];
23 }
24 };