function chunk(array, size) {
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
}
function compact(array) {
return array.filter(Boolean);
}
function concat(...arrays) {
return [].concat(...arrays);
}
function difference(array, ...values) {
const set = new Set([].concat(...values));
return array.filter(item => !set.has(item));
}
function drop(array, n = 1) {
return array.slice(n);
}
function dropRight(array, n = 1) {
return array.slice(0, -n);
}
function fill(array, value, start = 0, end = array.length) {
return array.map((item, index) => (index >= start && index < end) ? value : item);
}
function findIndex(array, predicate) {
for (let i = 0; i < array.length; i++) {
if (predicate(array[i], i, array)) {
return i;
}
}
return -1;
}
function flatten(array) {
return [].concat(...array);
}
function fromPairs(array) {
const result = {};
for (const [key, value] of array) {
result[key] = value;
}
return result;
}
function head(array) {
return array[0];
}
function indexOf(array, value, fromIndex = 0) {
for (let i = fromIndex; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}
return -1;
}
function initial(array) {
return array.slice(0, -1);
}
function intersection(...arrays) {
const set = new Set(arrays[0]);
for (const array of arrays) {
set.forEach(item => {
if (!array.includes(item)) {
set.delete(item);
}
});
}
return Array.from(set);
}
function join(array, separator = ',') {
return array.join(separator);
}
function last(array) {
return array[array.length - 1];
}
function nth(array, n = 0) {
return n >= 0 ? array[n] : array[array.length + n];
}
function pull(array, ...values) {
return array.filter(item => !values.includes(item));
}
function reverse(array) {
return array.reverse();
}
function slice(array, start = 0, end = array.length) {
return array.slice(start, end);
}
function sortedIndex(array, value) {
let low = 0;
let high = array.length;
while (low < high) {
const mid = Math.floorlow + high) / 2);
if (array[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
return low;
}
22.: 返回数组除了第一个元素的所有元素
function tail(array) {
return array.slice(1);
}
function take(array, n = 1) {
return array.slice(0, n);
}
function union(...arrays) {
return Array.from(new Set([].concat(...arrays)));
}
function uniq(array) {
return Array.from(new Set(array));
}
function without(array, ...values) {
return array.filter(item => !values.includes(item));
}
function xor(...arrays) {
const count = {};
for (const array of arrays) {
for (const item of array) {
count[item] = (count[item] || 0) + 1;
}
}
return Object.keys(count).filter(item => count[item] === 1);
}
function zip(...arrays) {
const maxLength = Math.max(...arrays.map(array => array.length));
const result = [];
for (let i = 0; i < maxLength; i++) {
result.push(arrays.map(array => array[i]));
}
return result;
}
function countBy(array, iteratee) {
const result = {};
for (const item of array) {
const key = typeof iteratee === 'function' ? iteratee(item) : item[iteratee];
result[key] = (result[key] || 0) + 1;
}
return result;
}
function debounce(func, wait, immediate = false) {
let timeout;
return function(...args) {
const later = () => {
timeout = null;
if (!immediate) {
func.apply(this, args);
}
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(this, args);
}
};
}