Code reorganization
[vchess.git] / client / src / utils / array.js
diff --git a/client/src/utils/array.js b/client/src/utils/array.js
new file mode 100644 (file)
index 0000000..a8466c6
--- /dev/null
@@ -0,0 +1,24 @@
+// Remove item(s) in array (if present)
+export function remove(array, rfun, all)
+{
+  const index = array.findIndex(rfun);
+  if (index >= 0)
+  {
+    array.splice(index, 1);
+    if (!!all)
+    {
+      // Reverse loop because of the splice below
+      for (let i=array.length-1; i>=index; i--)
+      {
+        if (rfun(array[i]))
+          array.splice(i, 1);
+      }
+    }
+  }
+}
+
+// Double array intialization
+export function init(size1, size2, initElem)
+{
+  return [...Array(size1)].map(e => Array(size2).fill(initElem));
+}