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

// => []

三、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。

7.3 实现

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

const arr = target;

const len = arr.length;

if (typeof callback !== ‘function’) {

throw new Error(‘need a function’)

}

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

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

if (callback(arr[i], i, arr)) {

return true;

}

}

return false;

}

7.4 测试

const arr = [1, 2, 3, 4, 5, 6];

最后

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

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

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

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
b前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

[外链图片转存中…(img-p5Ttelfk-1715774069570)]

[外链图片转存中…(img-nZCsZqeH-1715774069571)]

[外链图片转存中…(img-0PA7BMid-1715774069571)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值