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

创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

2.1 参数

  • function(currentValue,index,arr):

  • currentValue: 必须。当前元素的值

  • index: 可选。当前元素的索引值

  • arr: 可选。当前元素属于的数组对象

  • thisValue: 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,“this” 的值为 “undefined”

2.2 返回值

  • (Array): 返回通过检查指定数组中符合条件的所有元素

2.3 实现

Array.prototype._filter = function (callback, target = this) {

const arr = target; // 保存this

const result = []; // 返回新数组

const len = arr.length;

if (len === 0) return []; // 如果为空数组,返回[]

for (let i = 0; i < len; i++) {

if (callback(arr[i], i, arr)) result.push(arr[i]); //如果判定条件为true,保存至新数组

}

return result; // 返回符合条件的结果

};

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)

用于检测数组中的元素是否满足指定条件

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

最后

资料过多,篇幅有限,需要文中全部资料可以点击这里免费获取前端面试资料PDF完整版!

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

[外链图片转存中…(img-q7vwotNs-1713630637188)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

[外链图片转存中…(img-Mrr2jn8H-1713630637188)]

最后

[外链图片转存中…(img-47I1Bv4Y-1713630637188)]

[外链图片转存中…(img-4wOMtISN-1713630637188)]

资料过多,篇幅有限,需要文中全部资料可以点击这里免费获取前端面试资料PDF完整版!

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值