2.4 测试
const arr = [1, 2, 3, 4, 5, 6];
console.log(arr._filter((val) => val > 0))
// => [ 1, 2, 3, 4, 5, 6 ]
console.log(arr._filter((val) => val > 10))
// => []
_find(function(currentValue,index,arr), thisValue)
返回通过测试(函数内判断)的数组的第一个元素的值。
3.1 参数
-
function(currentValue,index,arr):
-
currentValue: 必须。当前元素的值
-
index: 可选。当前元素的索引值
-
arr: 可选。当前元素属于的数组对象
-
thisValue: 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,“this” 的值为 “undefined”
3.2 返回值
- (Any): 返回通过测试(函数内判断)的数组的第一个元素的值。
3.3 实现
Array.prototype._find = function (fn, target) {
const items = target || this;
for (let i = 0; i < items.length; i++) {
const cur = items[i];
const index = i;
const arr = items;
if (fn(cur, index, arr)) {
return cur;
}
}
return undefined;
};
3.4 测试
const ages = [3, 10, 18, 20];
console.log(ages._find((age) => age > 0)) // 3
console.log(ages._find((age) => age > 10)) // 18
forEach(collection, [iteratee=.identity])
调用 iteratee 遍历 collection(集合) 中的每个元素, iteratee 调用3个参数: (value, index, collection)。
4.1 参数
-
collection (Array): 一个用来迭代的集合。
-
[iteratee=_.identity] (Function): 每次迭代调用的函数。
4.2 返回值
- (*): 无
4.3 实现
const _forEach = (array = [], iteratee) => {
if (!array.length) return;
for (let i = 0; i < array.length; i++) {
iteratee(array[i], i, array);
}
};
4.4 测试
_forEach([1, 2, 3, 4, 5], function(value) {
console.log(value);
});
// => 1 2 3 4 5
_includes(collection, value, [fromIndex=0])
检查 value(值) 是否在 collection(集合) 中。如果 collection(集合)是一个字符串,那么检查 value(值,子字符串) 是否在字符串中, 否则做等值比较。 如果指定 fromIndex 是负数,那么从 collection(集合) 的结尾开始检索。
5.1 参数
-
collection (Array|string): 要检索的集合。
-
value (*): 要检索的值。
-
[fromIndex=0] (number): 要检索的 索引位置。
5.2 返回值
- (Boolean): 如果找到匹配的字符串返回 true,否则返回 false。
5.3 实现
const _includes = function (collection, value, fromIndex = 0) {
const { length } = collection;
if (!length) return false;
let index = fromIndex >= 0 ? fromIndex - 1 : length - 2;
if (typeof value === ‘string’) {
return collection.slice(++index).indexOf(value) !== -1
} else {
while (++index < length) {
if (collection[index] === value) {
return true
}
}
return false
}
};
5.4 测试
_includes([1, 2, 3], 1);
// => true
_includes([1, 2, 3], 1, 2);
// => false
_includes(‘pebbles’, ‘eb’);
// => true
forEach(collection, [iteratee=.identity])
创建一个数组, value(值) 是iteratee(迭代函数)遍历 collection(集合)中的每个元素后返回的结果。 iteratee(迭代函数)调用3个参数value, index, collection
6.1 参数
-
collection (Array): 一个用来迭代的集合。
-
[iteratee=_.identity] (Function): 每次迭代调用的函数。
6.2 返回值
- (Array): 返回新的映射后数组。
6.3 实现
const _map = (array = [], iteratee) => {
const { length } = array;
const result = new Array(length);
for (let i = 0; i < length; i++) {
result[i] = iteratee(array[i], i, array);
}
return result;
};
6.4 测试
console.log(_map([1, 2, 3, 4, 5], (val) => val + 1));
// => [2, 3, 4, 5, 6]
_some(function(currentValue,index,arr), thisValue)
用于检测数组中的元素是否满足指定条件
7.1 参数
-
function(currentValue,index,arr):
-
currentValue: 必须。当前元素的值
-
index: 可选。当前元素的索引值
-
arr: 可选。当前元素属于的数组对象
-
thisValue: 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,“this” 的值为 “undefined”
7.2 返回值
- (Bool): 如果数组中有元素满足条件返回 true,否则返回 false。
总结
-
框架原理真的深入某一部分具体的代码和实现方式时,要多注意到细节,不要只能写出一个框架。
-
算法方面很薄弱的,最好多刷一刷,不然影响你的工资和成功率😯
-
在投递简历之前,最好通过各种渠道找到公司内部的人,先提前了解业务,也可以帮助后期优秀 offer 的决策。
-
要勇于说不,对于某些 offer 待遇不满意、业务不喜欢,应该相信自己,不要因为当下没有更好的 offer 而投降,一份工作短则一年长则 N 年,为了幸福生活要慎重选择!!!
喜欢这篇文章文章的小伙伴们点赞+转发支持,你们的支持是我最大的动力!