📜 ⬆️ ⬇️

Javascript array traversal

Running through the elements of an array, choosing some elements from it, or creating a new array is necessary quite often. This can be done in the classic way:

 var arr = [1,2,3,4,5,6,7]
 var map = [];
 var subarray = [];
 for (var i = 0, len = arr.length; i <len; i ++) {
   var item = arr [i];
   // do something
   alert (item);
   // map and sub
   if (/ * some condition * /) {
     map.push (item +1);
     subarray.push (item)
   }
 }


However, in ECMA-262 [1] and most frameworks, alternative (more elegant) ways are supported to achieve this goal:
 var arr = [1,2,3,4,5,6,7]
 arr.forEach (function ((element, index, array) {alert (element);})
 var map = arr.map (function ((element, index, array) {
   if (/ * some condition * /)
     return element;
 })
 var subarray = arr.filter (function ((element, index, array) {return / * some condition * /})


You call array methods by passing them a function (closure, closure, lambda) which will be called for each array element with parameters (element / * array element * /, index / * index of this element * /, array / * array itself / /). In the case of the map and filter functions, new arrays are created that will be filled with what the function returns (in the map case) or those elements over which the function returns true (in the filter case).
')
The second parameter to these functions is to pass thisObject object which will be hidden behind this inside the function when it is executed) [ 1 ]:
 var writer = {
   sb: [],
   write: function (s) {
     this.sb.push (s);
   },
   writeln: function (s) {
     this.write (s + "\ n");
   },
   toString: function () {
      return this.sb.join ("");
   }
 };

 [2, 5, 9] .forEach (writer.writeln, writer);
 print (writer.toString ());  // assumes print is already defined

If the browser does not support these functions, they can be defined independently [ 2 ]:
 if (! Array.prototype.forEach)
 {
   Array.prototype.forEach = function (fun / *, thisp * /)
   {
     var len = this.length;
     if (typeof fun! = "function")
       throw new TypeError ();

     var thisp = arguments [1];
     for (var i = 0; i <len; i ++)
     {
       if (i in this)
         fun.call (thisp, this [i], i, this);
     }
   };
 }

 if (! Array.prototype.filter)
 {
   Array.prototype.filter = function (fun / *, thisp * /)
   {
     var len = this.length;
     if (typeof fun! = "function")
       throw new TypeError ();

     var res = new Array ();
     var thisp = arguments [1];
     for (var i = 0; i <len; i ++)
     {
       if (i in this)
       {
         var val = this [i];  // in case fun mutates this
         if (fun.call (thisp, val, i, this))
           res.push (val);
       }
     }

     return res;
   };
 }
 if (! Array.prototype.map)
 {
   Array.prototype.map = function (fun / *, thisp * /)
   {
     var len = this.length;
     if (typeof fun! = "function")
       throw new TypeError ();

     var res = new Array (len);
     var thisp = arguments [1];
     for (var i = 0; i <len; i ++)
     {
       if (i in this)
         res [i] = fun.call (thisp, this [i], i, this);
     }

     return res;
   };
 }

Among frameworks the most competent support of these functions is made in dojo. Where the names of the functions coincide with the specification and are redefined if not supported. In the rest (Ext, jquery, prototype) these functions are also supported under different names and with different variations.

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


All Articles