归纳整理JavaScript数组实例的9个方法
2310
本篇文章给大家带来了关于javascript的相关知识,主要介绍了JavaScript数组实例的9个方法,文章围绕主题展开详细的内容介绍没具有一定的参考价值,需要的朋友可以参考一下。
前言
手写JS原生API在面试中很常见,今天努力工作之余(摸鱼的时候)翻到了MDN文章中关于数组实例方法这部分,正好无聊就手写几个实例方法玩玩,复习一下基础内容,并记录一下。
如果你还不知道数组实例中迭代方法有什么区别,可以看下面这张图:
map
这个方法会返回一个新的数组,数组中的每一项都是执行过map
提供的回调函数结果。
实现代码如下:
- const map = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- // 定义一个空数组,用于存放修改后的数据
- let res = []
- for (let i = 0; i < array.length; i++) {
- res.push(fun(array[i]))
- }
- return res
- }
- // 测试
- let res = map([1, 2, 3], item => {
- return item * 2
- })
- console.log(res) // [ 2, 4, 6 ]
filter
这个方法会返回一个新的数组,数组中的值是满足filter
提供的回调函数的值,
实现代码如下:
- const filter = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- // 定义一个空数组,用于存放符合条件的数组项
- let res = []
- for (let i = 0; i < array.length; i++) {
- // 将数组中的每一项都调用传入的函数,如果返回结果为true,则将结果push进数组,最后返回
- fun(array[i]) && res.push(array[i])
- }
- return res
- }
- // 测试
- let res = filter([1, 2, 3], item => {
- return item > 2
- })
- console.log(res) // [ 3 ]
some
该方法会判断数组中的每一项,如果有一项满足回调函数中条件就返回true
都不满足则返回false
。
实现代码如下:
- const some = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- let flag = false
- for (let i of array) {
- if (fun(i)) {
- flag = true
- break
- }
- }
- return flag
- }
- let res = some([1, 2, 3], item => {
- return item > 2
- })
- console.log(res) // true
every
该方法会判断数组中的每一项,如果所有项满足回调函数中条件就返回true
否则返回false
。
实现代码如下:
- const every = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- let flag = true
- for (let i of array) {
- if (!fun(i)) {
- flag = false
- break
- }
- }
- return flag
- }
- let res = every([1, 2, 3], item => {
- return item > 0
- })
- console.log(res) // true
reduce
该方法会让数组中的每个元素执行我们提供的回调函数,并将结果汇总返回
实现代码如下:
- const reduce = (array, fun, initialValue) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- let accumulator = initialValue
- for (let i = 0; i < array.length; i++) {
- accumulator = fun(accumulator, array[i], i, array)
- }
- return accumulator
- }
- const arr = [1, 2, 3]
- console.log(arr.reduce(v => v + 10, 10)) // 40
- console.log(reduce(arr, v => v + 10, 10)) // 40
forEach
这个方法比较简答了,就是遍历数组方法,数组中的每一项都执行回调函数
实现代码如下:
- const forEach = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- for (let i of array) {
- fun(i)
- }
- }
- let res = forEach([1, 2, 3], item => {
- console.log(item)
- })
forEach
这个方法比较简答了,就是遍历数组方法,数组中的每一项都执行回调函数
实现代码如下:
- const forEach = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- for (let i of array) {
- fun(i)
- }
- }
- let res = forEach([1, 2, 3], item => {
- console.log(item)
- })
find和findIndex
这两个方法比较类似,一个返回元素,一个返回元素的索引,这里就编写一个
实现代码如下:
- const myFind = (array, fun) => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function')
- let res
- for (let i = 0; i < array.length; i++) {
- if (fun(array[i])) {
- res = array[i]
- }
- }
- return res
- }
- // 测试
- let res = myFind([1, 2, 3], item => {
- return item > 2
- })
- console.log(res) // 3
join
该方法可以将数组中的所有元素根据指定的字符串进行拼接,并返回拼接后的字符串,
实现代码如下:
- const join = (array, separator = ',') => {
- // 类型约束
- if (Object.prototype.toString.call(array) !== '[object Array]')
- throw new TypeError(array + ' is not a array')
- if (typeof separator !== 'string')
- throw new TypeError(separator + ' is not a string')
- let res = array[0].toString()
- for (let i = 0; i < array.length - 1; i++) {
- res += separator + array[i + 1].toString()
- }
- return res
- }
- // 测试
- let res = join([1, 2, 3], '-')
- console.log(res) // 1-2-3
本文网址:https://www.zztuku.com/detail-13008.html
站长图库 - 归纳整理JavaScript数组实例的9个方法
申明:本文转载于《脚本之家》,如有侵犯,请 联系我们 删除。
您还没有登录,请 登录 后发表评论!
提示:请勿发布广告垃圾评论,否则封号处理!!