This article describes the method of traversing arrays in JavaScript. Share it for your reference, as follows:
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="UTF-8"><title></title></head><body><script>var a = [1,2,3,4,5,6];var b = a.some(function(ele,index,arr){ console.log(ele);//Output 1,2,3 traversal to 3 stop traversal return ele > 2;});console.log(b);console.log("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ console.log(ele);//Output 1, 2, 3, 4, 5 traversing to 5 and returning false, stop traversing return ele < 5;});console.log(c);console.log("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- console.log(ele);//Transweep through the entire array, perform an operation on each element of the array, and add a new element to the e array return ele+3;});console.log(e);console.log("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------some : Return true as long as there is an option in the array that meets the conditions, no longer traverses the remaining elements. If all items do not meet the conditions, return false
every : as long as there is an option in the array that does not meet the conditions, it will return false, and no longer traverse the remaining elements. If all items meet the conditions, it will return true
filter : Filter array, this function returns a new array, pushes elements that meet the conditions in the original array into the new array
map : executes a callback function once on each element in the array and returns a new element to the new array
forEach : executes a callback function once on each element in the array, which does not return a value
The renderings are as follows:
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript Array Operation Skills", "Summary of JavaScript Traversal Algorithm and Skills", "Summary of JavaScript Mathematical Operation Usage", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills" and "Summary of JavaScript Errors and Debugging Skills"
I hope this article will be helpful to everyone's JavaScript programming.