1.forEach():让每一个数组的元素执行一次回调函数
2.map():返回一个由回调函数的返回值组成的新数组
3.filter():返回一个由数组中符合筛选条件的元素组成的新数组
4.find():返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined
5.findIndex():返回第一个在数组中符合筛选条件的元素的索引值并返回,找不到返回-1
6.every():检查是否数组每一个元素都符合条件,则返回true。反之返回false
7.some():检查是否数组至少有一个元素符合条件,则返回true。如果所有元素不符合条件,则返回false
8.reduce():返回数组中前项和后项做某种计算后并累计的值
1.forEach():让每一个数组的元素执行一次回调函数
/** *方法:forEach *作用:让每一个数组的元素执行一次回调函数 *语法:arr.forEach(callback(currentValue[, index[,array]])[, thisArg]) *参数: * callback 生成新数组元素的函数,使用三个参数 * currentValue 可选 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 forEach()方法正在操作的数组 * thisA 可选参数 当执行回调函数callback时,用作this的值 *返回值:undefined *描述:forEach 除了抛出异常以外,无法中止或者跳出循环 * */ const arr = ['a', 'b', 'c']; arr.forEach(function(e){ console.log(e) }) // "a" // "b" // "c" const arr = [1,3,7] let numAll = 0; arr.forEach(function(e){ numAll += e }) console.log(numAll) // 11
2.map():返回一个由回调函数的返回值组成的新数组
/** *方法:map *作用:返回一个由回调函数的返回值组成的新数组 *语法:var new_array = arr.map(function callback(currentValue[, index[, array]]) { * // Return element for new_array * }[, thisArg]) *参数: * callback 数组的每个元素执行的函数 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 map 方法正在调用的数组 * thisA 可选参数 执行callback函数时,用作this *返回值:一个由原数组每个元素执行回调函数的结果组成的新数组 *描述:map 不修改原数组 * 要写返回值 */ // 数组中每个元素乘2,返回新数组 const arr =[1,4,9,16] const arr1 = arr.map(x => x *2) console.log(arr1) // [2, 8, 18, 32] // 数组中的每个元素平方根 var numbers = [1,4,9] var roots = numbers.map(Math.sqrt) console.log(roots) // [1,2,3]
3.filter():返回一个由数组中符合筛选条件的元素组成的新数组
/** *方法:filter *作用:返回一个由数组中符合筛选条件的元素组成的新数组 *语法:var newArray = arr.filter(callback(currentValue[, index[, array]])[, thisArg]) *参数: * callback 数组的每个元素执行的函数 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 filter 方法正在调用的数组 * thisArg 可选参数 执行callback函数时,用作this *返回值:返回一个由数组中符合筛选条件的元素组成的新数组,如果都不符合条件,则返回空数组 *描述:filter 不修改原数组 * 要写返回值 */ // 筛选字符串长度大于6的元素组成一个新数组 const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'] const result = words .filter(word => word.length > 6) console.log(result) // ["exuberant", "destruction", "present"] var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange']; function filterItems(query){ return fruits.filter(function(e){ // 返回由某个指定的字符串可以在字符串(元素)中首次出现的元素组成的新数组 // 没出现indexOf 返回 -1 return e.toLowerCase().indexOf(query.toLowerCase()) > -1; }) } console.log(filterItems('AP')) console.log(filterItems('aN')) // ["apple", "grapes"] // ["banana", "mango", "orange"]
4.find():返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined
/** *方法:find *作用:返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined *语法:arr.find(callback(currentValue[, index[, array]])[, thisArg]) *参数: * callback 数组的每个元素执行的函数 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 find 方法正在调用的数组 * thisArg 可选参数 执行callback函数时,用作this *返回值:返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined *描述:find 不修改原数组 * 要写返回值 */ const arr = [5, 12, 8, 130, 44]; const arr1 = arr.find(element => element > 10) const arr2 = arr.find(element => element > 500) console.log(arr) console.log(arr1) console.log(arr2) // [5, 12, 8, 130, 44]; // 12 // undefined var inventory = [ {name: 'apples', quantity: 2}, {name: 'bananas', quantity: 0}, {name: 'cherries', quantity: 5} ]; function findItem(query){ return query.name = 'apples' } console.log(inventory.find(findItem)) console.log(inventory.find(e => e.name = 'apples')) // {name: "apples", quantity: 2} // {name: "apples", quantity: 2}
5.findIndex():返回第一个在数组中符合筛选条件的元素的索引值并返回,找不到返回-1
/** *方法:findIndex *作用:返回第一个在数组中符合筛选条件的元素的索引值并返回,找不到返回-1 *语法:arr.findIndex(callback(currentValue[, index[, array]])[, thisArg]) *参数: * callback 数组的每个元素执行的函数 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 findIndex 方法正在调用的数组 * thisArg 可选参数 执行callback函数时,用作this *返回值:返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined *描述:findIndex 不修改原数组 * 要写返回值 */ const arr = [5, 12, 8, 130, 44]; const arr1 = arr.findIndex(element => element > 10) const arr2 = arr.findIndex(element => element > 500) console.log(arr) console.log(arr1) console.log(arr2) // [5, 12, 8, 130, 44]; // 1 // -1
6.every():检查是否数组每一个元素都符合条件,则返回true。反之返回false
/** *方法:every *作用:检查是否数组每一个元素都符合条件,则返回true。反之返回false *语法:arr.every(callback(currentValue[, index[, array]])[, thisArg]) *参数: * callback 数组的每个元素执行的函数 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 every 方法正在调用的数组 * thisArg 可选参数 执行callback函数时,用作this *返回值:返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined *描述:every 不修改原数组 * 要写返回值 */ const arr = [1, 30, 39, 29, 10, 13]; const arr1 = arr.every(element => element > 0) const arr2 = arr.every(element => element > 10) console.log(arr) console.log(arr1) console.log(arr2) // [5, 12, 8, 130, 44]; // true // false
7.some():检查是否数组至少有一个元素符合条件,则返回true。如果所有元素不符合条件,则返回false
/** *方法:some *作用:检查是否数组至少有一个元素符合条件,则返回true。如果所有元素不符合条件,则返回false *语法:arr.every(callback(currentValue[, index[, array]])[, thisArg]) *参数: * callback 数组的每个元素执行的函数 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 some 方法正在调用的数组 * thisArg 可选参数 执行callback函数时,用作this *返回值:返回第一个在数组中符合筛选条件的元素并返回,找不到返回undefined *描述:some 不修改原数组 * 要写返回值 */ const arr = [1, 30, 39, 29, 10, 13]; const arr1 = arr.some(element => element > 0) const arr2 = arr.some(element => element > 10) console.log(arr) console.log(arr1) console.log(arr2) // [5, 12, 8, 130, 44]; // true // true
8.reduce():返回数组中前项和后项做某种计算后并累计的值
/** *方法:reduce *作用:返回数组中前项和后项做某种计算后并累计的值 *语法:arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue]) *参数: * callback 数组的每个元素执行的函数 * accumulator 累计器累计回调的返回值 * currentValue 数组中正在处理的当前元素 * index 可选 数组中正在处理当前元素的索引 * array 可选 reduce 方法正在调用的数组 * initialValue 作为第一次调用 callback函数时的第一个参数的值。 * 如果没有提供初始值,则将使用数组中的第一个元素。 * 在没有初始值的空数组上调用 reduce 将报错。 *返回值:返回函数累计的结果 *描述:reduce 不修改原数组 * 要写返回值 */ const arr = [1,2,3,4] const arr1 = arr.reduce((el1,el2) => el1 + el2) const arr2 = arr.reduce((el1,el2) => el1 * el2) console.log(arr) console.log(arr2) // [1,2,3,4] // 10 // 24