Array.prototype.forEach()
定义
forEach()
方法对数组的每个元素执行一次给定的函数。
语法
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
示例
手写一个forEach()
Array.prototype.myForeach = function(fn) {
if (!Object.prototype.toString.call(fn) === '[object Function]') {
throw new Error(`${fn} is not a function`)
}
let param = this
for (let i = 0; i < param.length; i++) {
fn(param[i], i, param)
}
}
let arr = ['a', 'b', 'c']
arr.myForeach((item, index) => {
console.log(item, index)
});
Array.prototype.map()
定义
map()
方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。
语法
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// Return element for new_array
}[, thisArg])
示例
手写一个map()
Array.prototype.myMap = function(fn) {
if (!Object.prototype.toString.call(fn) === '[object Function]') {
throw new Error(`${fn} is not a function`)
}
let param = this
let newMap = []
for (let i = 0; i < param.length; i++) {
newMap.push(fn(param[i], i, param))
}
return newMap
}
let arr = ['a', 'b', 'c']
let newArr = arr.myMap(item => {
return item + item
});
console.log(newArr)
Array.prototype.filter()
定义
filter()
方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
语法
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
示例
手写一个filter()
Array.prototype.myfilter = function(fn) {
if (!Object.prototype.toString.call(fn) === '[object Function]') {
throw new Error(`${fn} is not a function`)
}
let param = this
let filterArr = []
for (let i = 0; i < param.length; i++) {
if (fn(param[i], i, param)) {
filterArr.push(param[i])
}
}
return filterArr
}
let arr = ['a', 'b', 'c']
let newArr = arr.myfilter(item => {
return item === 'a'
});
console.log(newArr)
Array.prototype.every()
定义
every()
方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。
注意:若收到一个空数组,此方法在一切情况下都会返回 true
。
语法
arr.every(callback(element[, index[, array]])[, thisArg])
示例
手写一个every()
Array.prototype.myEvery = function(fn) {
if (!Object.prototype.toString.call(fn) === '[object Function]') {
throw new Error(`${fn} is not a function`)
}
let param = this
for (let i = 0; i < param.length; i++) {
if (!fn(param[i], i, param)) {
return false
}
}
return true
}
let arr = ['a', 'a', 'a']
let newArr = arr.myEvery(item => {
return item === 'a'
});
console.log(newArr)
Array.prototype.some()
定义
some()
方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。
注意:如果用一个空数组进行测试,在任何情况下它返回的都是false
。
语法
arr.some(callback(element[, index[, array]])[, thisArg])
示例
手写一个some()
Array.prototype.mySome = function(fn) {
if (!Object.prototype.toString.call(fn) === '[object Function]') {
throw new Error(`${fn} is not a function`)
}
let param = this
for (let i = 0; i < param.length; i++) {
if (fn(param[i], i, param)) {
return true
}
}
return false
}
let arr = ['b', 'a', 'c']
let newArr = arr.mySome(item => {
return item === 'a'
});
console.log(newArr)
Array.prototype.find()
定义
find()
方法返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined。
示例
手写一个find()
Array.prototype.myFind =function (fn) {
if(!Object.prototype.toString.call(fn) === '[object Function]'){
throw new Error(`${fn} is not a funcion`)
}
let param = this
for(let i=0;i<param.length;i++){
if(fn(param[i], i, param)) {
return param[i]
}
}
}
console.log(arr.myFind(item => item > 2))