Array.prototype.forEach method;for loop;for...in loop.for...of (implicit use of an iterator); var a = ["a", "b", "c"]; a.forEach(function(entry) { console.log(entry); }); forEach requires the connection of the es5-shim emulation library for browsers that do not have native support for this method. These include IE 8 and earlier, which are still used in some places.forEach include the fact that there is no need to declare local variables to store the index and the value of the current element of the array, since they are automatically passed to the callback function (callback) as arguments.forEach designed to iterate through all the elements of an array, but apart from it, ES5 offers some more useful methods for iterating over all or some elements, plus performing some actions on them:every - returns true , if for each element of the array the colback returns a value that is true .some - returns true , if at least for one element of the array, a colback returns a value that is true .filter - creates a new array, including those elements of the original array, for which a colback returns true .map - creates a new array consisting of the values ​​returned by the callback.reduce - reduces the array to a single value, applying a colback in turn to each element of the array, starting with the first (it can be useful for calculating the sum of the elements of the array and other resulting functions).reduceRight - works like reduce, but goes through the elements in the reverse order.for taxis : var a = ["a", "b", "c"]; var index; for (index = 0; index < a.length; ++index) { console.log(a[index]); } for with storing the length of the array: var a = ["a", "b", "c"]; var index, len; for (index = 0, len = a.length; index < len; ++index) { console.log(a[index]); } var a = ["a", "b", "c"]; var index; for (index = a.length - 1; index >= 0; --index) { console.log(a[index]); } for...in loop, remember that iterating over arrays is not what it is intended for . Contrary to the common misconception, the for...in loop loops through not the array indices, but enumerated properties of the object.for...in may be useful, if only you follow the precautions, as shown in the example below: // a - var a = []; a[0] = "a"; a[10] = "b"; a[10000] = "c"; for (var key in a) { if (a.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) { console.log(a[key]); } } key (not inherited from its prototype).key is a string containing a decimal notation of an integer whose value is less than 4294967294 . Where does the last number come from? From the definition of the array index in ES5, from which it follows that the largest index that an element in an array can have: (2^32 - 2) = 4294967294 .for loop, since in this case only those elements that are explicitly defined in the array are iterated. So, in the example above, only 3 iterations will be executed (for indices 0, 10 and 10000) - against 10001 in the for loop. function arrayHasOwnIndex(array, key) { return array.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294; } for (key in a) { if (arrayHasOwnIndex(a, key)) { console.log(a[key]); } } for (key in a) { if (a.hasOwnProperty(key) && String(parseInt(key, 10)) === key) { console.log(a[key]); } } next() method is defined - a function with no arguments, which returns an object with two properties:done ( boolean ) - true if the iterator has reached the end of the iterated sequence. Otherwise false .value - determines the value returned by the iterator. May not be defined (absent) if the done property is true .for...of construct.for...of : var val; var a = ["a", "b", "c"]; for (val of a) { console.log(val); } for...of loop implicitly calls the Array object's iterator to get each value of the array.for...of loop. It looks like this: var a = ["a", "b", "c"]; var it = a.entries(); var entry; while (!(entry = it.next()).done) { console.log(entry.value[1]); } Array.prototype.entries method returns an iterator that is used to output the values ​​of the array. At each iteration, entry.value contains an array of the form [, ] .length property and properties with names in the form of numbers that correspond to the elements of the array. Examples include the DOM collection of the NodeList and the pseudo- NodeList arguments , available within any function / method.for and for...in constructions can be applied to array-like objects in exactly the same way as with real arrays.forEach and other Array.prototype methods also apply to array-like objects. To do this, use the call Function.call or Function.apply .forEach to the childNodes property of the Node object, this is done like this: Array.prototype.forEach.call(node.childNodes, function(child) { // - child }); Array.prototype.forEach method in a separate variable and use it as an abbreviation: // (, ) var forEach = Array.prototype.forEach; // ... forEach.call(node.childNodes, function(child) { // - child }); Array.prototype.slice , which can be applied to any array-like object. This is done very simply, as shown in the example below: var trueArray = Array.prototype.slice.call(arrayLikeObject, 0); NodeList collection into a real array, you need something like this code: var divs = Array.prototype.slice.call(document.querySelectorAll("div"), 0); Array.prototype.slice you can use the more visual method Array.from .Array.prototype methods to Array.prototype objects (such as the DOM collection), then you should keep in mind that the correct operation of these methods is not guaranteed in all execution environments (including browsers). It depends on the behavior of a particular object in a particular execution environment, more precisely, on how the abstract operation HasProperty implemented in this object. The problem is that the ES5 standard itself allows for the possibility of an incorrect behavior of an object in relation to this operation (see §8.6.2 ).Array.prototype methods in each execution environment (browser) in which you plan to use your application.Source: https://habr.com/ru/post/247857/
All Articles