📜 ⬆️ ⬇️

We work with arrays in JavaScript without bicycles

I want to devote this article to the subtleties of working with arrays in JavaScript.

Probably, every JavaScript developer has functions (self-written or library) $ (selection of an element by id) and $$ (selection of elements by a CSS class). If we select several elements in the CSS class, then we want to perform certain actions with them. The loop suggests itself. And if you need a sample among them? Transformations? Actions over the necessary elements? Uneasy code is obtained.

JavaScript does not stand still, Mozilla constantly improves JavaScript in its Gecko engine and it's a sin not to use these innovations. JavaScript 1.6 adds new useful methods to simplify the code needed in the situations described above.

New Array Class Methods


filter

filter (callback [, thisObject])
A selection of items according to certain criteria.
')
Example
var myArray = [3, 5, 7, 9, 4, 8, 2, 1, 6];
var newArray = myArray.filter ( function (item) { return item> 5;});
// newArray will be equal to the array [7, 9, 8, 6]


forEach

forEach (callback [, thisObject])
Perform actions on each of the elements of the array.

Example
var myArray = [3, 5, 7, 9, 4, 8, 2, 1, 6];
myArray.forEach ( function (item) {alert (item);});


every

every (callback [, thisObject])
Returns true if all elements of the array satisfy the condition, otherwise returns false .

Example
var myArray = [3, 5, 7, 9, 4, 8, 2, 1, 6];
alert (myArray.every ( function (item) { return item> 5;})); // false
alert (myArray.every ( function (item) { Return item <10;})); // true


some

some (callback [, thisObject])
Returns true at least one array element fulfills the condition, otherwise returns false .

Example
var myArray = [3, 5, 7, 9, 4, 8, 2, 1, 6];
alert (myArray.some ( function (item) { return item> 5;})); // true
alert (myArray.some ( function (item) { return item> 10;})); // false


map

map (callback [, thisObject])
Creates a new array, into which the elements of the old array fall after being processed by the specified function.

Example
var myArray = [3, 5, 7, 9, 4, 8, 2, 1, 6];
var newArray = myArray.map ( function (item) { return item + 10;});
// newArray will be equal to the array [13, 15, 17, 19, 14, 18, 12, 11, 16]


We combine


These methods can be used in combination with each other.

Example
var myArray = [3, 5, 7, 9, 4, 8, 2, 1, 6];
myArray
.filter ( function (item) { return item> 5;})
.forEach ( function (item) {alert (item);});


Closer to practice
document.forms [0] .elements
.filter ( function (item) { return item.type == 'checkbox';})
.forEach ( function (item) {item.checked = true;});


Compatibility


Well, we read this article and were inspired ... but damn it! This is only in JavaScript from version 1.6!

No problem. Developers from Mozilla to each of these functions wrote an implementation for older versions of JavaScript and laid out in the description of the functions.

I compiled these implementations into the arrays.js file.

Good luck in your development!

PS Do not write that in jQuery all this is :)

Source: https://habr.com/ru/post/28977/


All Articles