Fix Eightpieces
[vchess.git] / client / src / utils / array.js
CommitLineData
8d61fc4a 1// Remove item(s) in array (if present)
6808d7a1 2export const ArrayFun = {
937c24ab 3
6808d7a1 4 remove: function(arr, rfun, all) {
9d58ef95 5 const index = arr.findIndex(rfun);
6808d7a1 6 if (index >= 0) {
9d58ef95 7 arr.splice(index, 1);
6e0f2842 8 if (!!all) {
e2732923 9 // Reverse loop because of the splice below
6808d7a1
BA
10 for (let i = arr.length - 1; i >= index; i--) {
11 if (rfun(arr[i])) arr.splice(i, 1);
e2732923 12 }
8d61fc4a
BA
13 }
14 }
e2732923 15 },
8d61fc4a 16
e2732923 17 // Double array intialization
6808d7a1
BA
18 init: function(size1, size2, initElem) {
19 return [...Array(size1)].map(() => Array(size2).fill(initElem));
e2732923 20 },
c66a829b 21
6808d7a1 22 range: function(max) {
e2732923 23 return [...Array(max).keys()];
6808d7a1 24 }
937c24ab 25
e2732923 26};