JS数组遍历相关

参考链接:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

  • Array.prototype.forEach()
	// 语法:arr .forEach(callback(currentValue [,index [,array]]) [,thisArg ]);
	/**
	 * MDN解释: The forEach() method executes a provided function once for each array element.
	 * 该 forEach() 方法对每个数组元素执行一次提供的函数。
 	 */
 	let arr = [1, 3, 4, 6]
 	arr.forEach( x => {
 		console.log(x+1)
 	})
 	// output:2, 4, 5, 7
  • Array.prototype.every()
	// 语法:arr.every(callback(element[, index[, array]])[, thisArg])
	/**
	 * MDN解释:The every() method tests whether all elements in the array pass the test implemented by the provided function.It returns a Boolean value.
	 * 该 every() 方法测试数组中的所有元素是否都通过了由提供的函数实现的测试。它返回一个布尔值。
	 * This method returns true for any condition put on an empty array.
	 * 此方法返回 true 放在空数组上的任何条件。
	 */
	// arr = [1, 3, 4, 6]
	let result1 = arr.every(x => x < 4)
	console.log(result1)  
	// output: false
  • Array.prototype.some()
	// 语法:arr.some(callback(element[, index[, array]])[, thisArg])
	/**
	* MDN解释: The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value. 
	* 该 some() 方法测试数组中的至少一个元素是否通过了由提供的函数实现的测试。它返回一个布尔值。
 	* This method returns false for any condition put on an empty array.
 	*  此方法返回 false 放在空数组上的任何条件。
 	*/
 	// arr = [1, 3, 4, 6]
	let result2 = arr.some(x => x < 4)
	console.log(result2)
	// output: true
  • Array.prototype.includes()
	// 语法:arr.includes(valueToFind[, fromIndex])
	/**
 	* MDN解释:The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
	* 该 includes()方法确定数组是否在其条目中包括特定值,返回 true 或 false 适当时。
	*/
	// arr = [1, 3, 4, 6]
	let result3 = arr.includes(4)
	console.log(result3)  
	// output:true
  • Array.prototype.indexOf()
	// 语法:arr.indexOf(searchElement[, fromIndex])
	/**
	* MDN解释:The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
 	* 该 indexOf()方法返回可在数组中找到给定元素的第一个索引,如果不存在则返回-1。
 	*/
 	// arr = [1, 3, 4, 6]
	let result4 = arr.indexOf(4)
	console.log(result4)  
	// output:2
  • Array.prototype.find()
	// 语法:arr.find(callback(element[, index[, array]])[, thisArg])
	/**
	* MDN解释:The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
 	* 该 find()方法返回在数组中第一个满足提供的测试功能的元素。否则返回 undefined。
	*/
	// arr = [1, 3, 4, 6]
	let result5 = arr.find(x => x < 4)
	console.log(result5)  
	// output: 1
  • Array.prototype.findIndex()
	// 语法:arr.findIndex(callback(element[, index[, array]])[, thisArg])
	/**
 	* MDN解释:The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
 	* 该 find()方法返回在数组中第一个满足提供的测试功能的元素的索引。否则返回 -1,表面没有元素通过测试。
 	*/
 	// arr = [1, 3, 4, 6]
	let result6 = arr.findIndex(x => x < 4)
	console.log(result6)  
	// output:0
  • Array.prototype.map()
	/*语法:
  	var new_array = arr.map(function callback(currentValue[, index[, array]]) {
    	// Return element for new_array
  	}[, thisArg])
	*/
	/**
 	 * MDN解释:The map() method creates a new array with the results of calling a provided function on every element in the calling array.
 	 * 该 map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。
 	 */
 	 // arr = [1, 3, 4, 6]
	let result7 = arr.map(x => x * 2)
	console.log(result7)  
	// output: [2, 6, 8, 12]
  • Array.prototype.filter()
	// 语法:var newArray = arr .filter(callback(element [,index [,array]]) [,thisArg ])
	/**
 	 * MDN解释:The filter() method creates a new array with all elements that pass the test implemented by the provided function.
 	 * 该filter()方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
 	*/
 	// arr = [1, 3, 4, 6]
	let result8 = arr.filter(x => x > 3)
	console.log(result8)
	// output: [4, 6]
  • Array.prototype.reduce()
	// 语法:arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
	// accumulator 累加器
	/**
 	 * MDN解释:The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
 	 * 该 reduce()方法对数组的每个元素执行reducer函数(您提供),从而生成单个输出值。
 	 */
	// arr = [1, 3, 4, 6]
	let result9 = arr.reduce((x, y) => x + y)
	console.log(result9)  
	// output: 14
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值