总结整理JavaScript数组实例的9个方法(javascript数组操作)
admin1176156年前0条评论
给人人带来了对于于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 {
- 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 {
- 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 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 {
- 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
本文链接:https://addon.ciliseo.com/zong-jie-zheng-li-javascript-shu-zu-shi-li-de-9-ge-fang-fa.html
网友评论