所有分类
  • 所有分类
  • 织梦模板

总结整理JavaScript数组实例的9个方法

给大家带来了关于javascript的相关知识,主要介绍了JavaScript数组实例的9个方法,文章围绕主题展开详细的内容介绍没具有一定的参考价值,需要的朋友可以参考一下。

归纳整理JavaScript数组实例的9个方法

前言

手写JS原生API在面试中很常见,今天努力工作之余(摸鱼的时候)翻到了MDN文章中关于数组实例方法这部分,正好无聊就手写几个实例方法玩玩,复习一下基础内容,并记录一下。

归纳整理JavaScript数组实例的9个方法

如果你还不知道数组实例中迭代方法有什么区别,可以看下面这张图:

归纳整理JavaScript数组实例的9个方法

map

这个方法会返回一个新的数组,数组中的每一项都是执行过map提供的回调函数结果。

实现代码如下:

  1. const map = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.  
  7.   // 定义一个空数组,用于存放修改后的数据
  8.   let res = []
  9.   for (let i = 0; i < array.length; i++) {
  10.     res.push(fun(array[i]))
  11.   }
  12.   return res
  13. }
  14. // 测试
  15. let res = map([1, 2, 3], item => {
  16.   return item * 2
  17. })
  18. console.log(res) // [ 2, 4, 6 ]

filter

这个方法会返回一个新的数组,数组中的值是满足filter提供的回调函数的值,

实现代码如下:

  1. const filter = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.  
  7.   // 定义一个空数组,用于存放符合条件的数组项
  8.   let res = []
  9.   for (let i = 0; i < array.length; i++) {
  10.     // 将数组中的每一项都调用传入的函数,如果返回结果为true,则将结果push进数组,最后返回
  11.     fun(array[i]) && res.push(array[i])
  12.   }
  13.   return res
  14. }
  15. // 测试
  16. let res = filter([1, 2, 3], item => {
  17.   return item > 2
  18. })
  19. console.log(res) // [ 3 ]

some

该方法会判断数组中的每一项,如果有一项满足回调函数中条件就返回true都不满足则返回false

实现代码如下:

  1. const some = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.   let flag = false
  7.   for (let i of array) {
  8.     if (fun(i)) {
  9.       flag = true
  10.       break
  11.     }
  12.   }
  13.   return flag
  14. }
  15. let res = some([1, 2, 3], item => {
  16.   return item > 2
  17. })
  18. console.log(res) // true

every

该方法会判断数组中的每一项,如果所有项满足回调函数中条件就返回true否则返回false

实现代码如下:

  1. const every = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.   let flag = true
  7.   for (let i of array) {
  8.     if (!fun(i)) {
  9.       flag = false
  10.       break
  11.     }
  12.   }
  13.   return flag
  14. }
  15. let res = every([1, 2, 3], item => {
  16.   return item > 0
  17. })
  18. console.log(res) // true

reduce

该方法会让数组中的每个元素执行我们提供的回调函数,并将结果汇总返回

实现代码如下:

  1. const reduce = (array, fun, initialValue) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.   let accumulator = initialValue
  7.   for (let i = 0; i < array.length; i++) {
  8.     accumulator = fun(accumulator, array[i], i, array)
  9.   }
  10.   return accumulator
  11. }
  12. const arr = [1, 2, 3]
  13. console.log(arr.reduce(=> v + 10, 10)) // 40
  14. console.log(reduce(arr, v => v + 10, 10)) // 40

forEach

这个方法比较简答了,就是遍历数组方法,数组中的每一项都执行回调函数

实现代码如下:

  1. const forEach = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.  
  7.   for (let i of array) {
  8.     fun(i)
  9.   }
  10. }
  11. let res = forEach([1, 2, 3], item => {
  12.   console.log(item)
  13. })

forEach

这个方法比较简答了,就是遍历数组方法,数组中的每一项都执行回调函数

实现代码如下:

  1. const forEach = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.  
  7.   for (let i of array) {
  8.     fun(i)
  9.   }
  10. }
  11. let res = forEach([1, 2, 3], item => {
  12.   console.log(item)
  13. })

find和findIndex

这两个方法比较类似,一个返回元素,一个返回元素的索引,这里就编写一个

实现代码如下:

  1. const myFind = (array, fun) => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
  6.   let res
  7.   for (let i = 0; i < array.length; i++) {
  8.     if (fun(array[i])) {
  9.       res = array[i]
  10.     }
  11.   }
  12.   return res
  13. }
  14. // 测试
  15. let res = myFind([1, 2, 3], item => {
  16.   return item > 2
  17. })
  18. console.log(res) // 3

join

该方法可以将数组中的所有元素根据指定的字符串进行拼接,并返回拼接后的字符串,

实现代码如下:

  1. const join = (array, separator = ',') => {
  2.   // 类型约束
  3.   if (Object.prototype.toString.call(array) !== '[object Array]')
  4.     throw new TypeError(array + ' is not a array')
  5.   if (typeof separator !== 'string')
  6.     throw new TypeError(separator + ' is not a string')
  7.   let res = array[0].toString()
  8.   for (let i = 0; i < array.length - 1; i++) {
  9.     res += separator + array[+ 1].toString()
  10.   }
  11.   return res
  12. }
  13. // 测试
  14. let res = join([1, 2, 3], '-')
  15. console.log(res) // 1-2-3
常见问题
所有VIP可以商用吗,会不会有版权问题?
本站提供的源码大部分可以作为源码开发参考和学习,部分开源可商用。
账号可以分享给其他人使用吗?
您好,账号禁止共享,我们有验证机制的,会自动封号处理,谢谢配合。
如果遇到问题怎么联系解决呢?
最快的途径是在导航菜单选择提交【售后】会在24小时处理
下载的源码可以使用吗?
我们会在显著的地方表明【已测试】,显示已测试的都是小编测试过 。
1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
5.如有侵权请联系客服邮件770973008@qq.com
原文链接:https://www.56admin.com/26812.html,转载请注明出处。
0
分享海报

评论0

请先
九年老站,质量有保证,90%亲自测试!优惠原价199模板币终生SVIP,优惠价68模板币! 数量有限! 购买SVIP 所有资源免积分下载,畅享无忧!
显示验证码
没有账号?注册  忘记密码?