The following small series will introduce you to the array traversal forEach () and map () methods in JavaScript and the compatible writing method. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at it with xiaobian.
• Principle:
• Advanced browsers support the forEach
Translator by: I love the map more than Haskell.
Original: JavaScript?—? Map vs. Foreach-what ' s The difference between Map and ForEach in JavaScript?
Translator: Fundebug
the array pass the test of the specified function. Every () does not change the original array.Grammar:Arr.every (callback[, Thisarg])Parameters:1. Callback: A function executed on each item of the array, receiving three parameters:CurrentValue (the value of the current item), index (the current item's indexes), and array (the array itself);2. Thisarg: Optional parameter. An object that is used as the value of this in the callback function, that is, the execution context of the callback functio
remember them correctly. Similar to the following methods, such as $.map. var arr = [1,2,3,4]; var res= arr.foreach (function (Value,index,array) {Array[index] = value*4;}); Console.log (arr); The result is [4,8,12,16]console.log (res); Undefinedmap:map is the meaning of "mapping" similar to the use of ForEach, which is: [].map (function (Value,index,array) {//c
The following small series will bring you a forEach, $. each, and map method recommendation in JS. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at it. forEach is the most basic new Array method in ecma5. it is traversal and loop. For example:
[1, 2, 3, 4]. forEach
Well, unexpectedly can not single a picture, not enough 200 words ah not enough 200 words!In the JavaScript advanced programming, a very large number of methods are introduced in the category, among which there are 6 kinds of iterative methods, and these 6 methods have a very wide role in the actual project. I love using foreach and map, easy to use and efficient
. Map (FN) method1 var a=[2, 4, 6];2 a.map (function (d, I, arr) {3 return {x:i, y:d*i, z:arr};4});Output Result:1 [{x:0, y:0, Z:[2, 4, 6]}, 2 {x:1, Y:4, Z:[2, 4, 6]}, 3 {x:2, Y:12, Z:[2, 4, 6]}The d in the callback function is the value of the traversal array, and I is the index of the array, and the entire array is passed in the third parameter.Map casts the original array into a new array of the same length, and the value of each corresponding memb
This article mainly introduces 5 array methods: IndexOf, filter, ForEach, map, reduce use examples, need friends can refer to the followingThe ECMASCRIPT5 standard, published on December 3, 2009, brings a number of new ways to improve the operations of existing array arrays. However, these novel array methods are not really popular because of the lack of browsers supporting ES5.
Array "Extras"
No one doubts
1. Map loopLet Arr=[1,2,3,4];arr.map (function (Value,key,arr) {//value, index, array (default = selected array) return item; Returns an array of all undefined if no return, number is index +1 (key+1)})//returns an array2. Foreach LoopLet Arr=[1,2,3,4];arr.map (function (Value,key,arr) {//value, index, array (default = selected array) return item; Do anything})//return only undefined3. Filter loopLet
Recommended forEach, $. each, and map methods in JS, foreach. each
ForEach is the most basic of the new Array method in ecma5. it is traversal and loop. For example:
[1, 2, 3, 4]. forEach (alert );
Equivalent to the for loop below
var array = [1, 2, 3, 4];for (var k = 0, len
First, native JS ForEach () and map () traversalCommon:1. All loops iterate through each item in the array.Each execution of the anonymous function in 2.forEach () and map () supports 3 parameters: the current item in the array item, the index of the current item, and the original array input.3. This in the anonymous f
Array functions(both index and ARR in the callback function here can be omitted, the callback function has parameters that are set to adjust this point, which is not used here for the time being)
ForEach ()
Map ()--Update array
Filter (), includes (), find (), FindIndex ()--Filters (deletes) an array
Some (), every ()--judgment array
Reduce ()--overlay Array
Arr.foreach ()Itera
foreach is the most basic one of the new array methods in ECMA5, that is, traversal, loop. For example, the following example:
[1, 2, 3, 4].foreach (alert);
Equivalent to the following for loop
var array = [1, 2, 3, 4];
for (var k = 0, length = array.length k
Array in ES5 's new method, the parameters are function types, the default is the argument, the function callback in the
ForEach:
foreach is the most basic one of the new array methods in ECMA5, that is, traversal, loop. For example, the following example:[1, 2, 3, 4].foreach (alert);Equivalent to the following for loop:
var array = [1, 2, 3, 4];
for (var i = 0, length = Array.Length i
The function callback in the Foreach method suppo
Tag: false equals SAR dev Red initialize ice parsing newTalk about array traversal methodsThere are several ways to iterate the JS array:
Every
Some
Filter
Foreach
Map
Reduce
Next, we will exchange each other.Every ()
Arr.every (callback[, Thisarg])return Value: TRUE | FalseWhether to change the original array: Do not change the original array
Analytical:The E
The importance of arrays in each programming language is self-evident, but in previous JavaScript arrays (JavaScript arrays in detail) have been very powerful, but the methods of operation are not perfect, and they have been properly supplemented in ECMAScript5.Array.isarray (Element)This is a static function of the array object that is used to determine whether an object is not an arrayvar a = new Array (1
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.