// 数组包含
isContain: function (arr1, arr2) {
for (var i = arr2.length - 1; i >= 0; i--) {
if (!arr1.includes(arr2[i])) {
return false;
}
}
return true;
},
// 数组排序 isIncrease: true递增 小->大 | isIncrease: false递减 大->小
arrCompare: function(property, isIncrease) {
return function(a, b) {
var value1 = a[property];
var value2 = b[property];
if (isIncrease) {
return value1 - value2;
} else {
return value2 - value1;
}
};
},
二维数组转一维数组
const arr = [1,[2,3],[4,5]];
console.log([].concat.apply([],arr));
const resultArr = arr.flat();
数组乱序
/**
* 数组乱序
* @param {*} arr
*/
export const shuffle = arr => {
let m = arr.length;
while (m > 1) {
const index = Math.floor(Math.random() * m--);
[arr[m], arr[index]] = [arr[index], arr[m]];
}
return arr;
};
数组分割
/*
* 将一个数组分成几个同等长度的数组
* array[分割的原数组]
* size[每个子数组的长度]
*/
const sliceArray = (array, size) => {
let result = [];
for (let x = 0; x < Math.ceil(array.length / size); x++) {
const start = x * size;
const end = start + size;
const sliceList = array.slice(start, end);
result.push(sliceList);
}
result = result.flat();
return result;
};