2023/03/13
compact
/**
* _.compact([0, 1, false, 2, '', 3]);
* // => [1, 2, 3]
*/
function compact(array) {
var index = -1,
length = array == null ? 0 : array.length,
resIndex = 0,
result = [];
while (++index < length) {
var value = array[index];
if (value) {
result[resIndex++] = value;
}
}
return result;
}
my-compact
const compact = (arr) => {
const newArr = arr.filter(item => item)
return newArr
}
concat
function concat() {
var length = arguments.length;
if (!length) {
return [];
}
var args = Array(length - 1),
array = arguments[0],
index = length;
while (index--) {
args[index - 1] = arguments[index];
}
return arrayPush(isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1));
}
my-concat
const concat = (...args) => {
if (!args.length) return
const newArr = [...args]
return newArr.flat(1)
}
difference
my-difference
const difference = (array, filterArray) => {
const map = new Map()
filterArray.map(item => map.set(item, item))
const newArr = array.filter(item => {
if (item === map.get(item)) return
return item
})
return newArr
}
看不懂题目的我直接没写了,明天摸鱼再写。