Step toward a one-page application
[vchess.git] / public / javascripts / utils / array.js
1 // Remove item in array (if present)
2 var removeItem = function(array, rfun)
3 {
4 let index = array.findIndex(rfun);
5 if (index >= 0)
6 array.splice(index, 1);
7 }
8
9 // Remove several item matching a condition
10 var removeMultiple = function(array, rfun)
11 {
12 // Reverse loop because of the splice below
13 for (let i=array.length-1; i>=0; i--)
14 {
15 if (rfun(array[i]))
16 array.splice(i, 1);
17 }
18 }
19
20 // Double array intialization
21 var doubleArray = function(size1, size2, initElem)
22 {
23 return _.map(_.range(size1), () => {
24 return _.map(_.range(size2), () => {
25 return initElem; //can be undefined
26 })
27 });
28 }
29
30 var copyDoubleArray = function(arr)
31 {
32 return _.map(_.range(arr.length), (el1,i) => {
33 return _.map(_.range(arr[0].length), (el2,j) => {
34 return arr[i][j];
35 })
36 });
37 }