【值得收藏】面试会用到的七个常用的遍历方法JS实现(二)

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


_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


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


_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

六、map


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


_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 年,为了幸福生活要慎重选择!!!

喜欢这篇文章文章的小伙伴们点赞+转发支持,你们的支持是我最大的动力!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值