Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #34141

Re: Iterating through an array (special syntax)

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Iterating through an array (special syntax)
Date 2017-10-19 21:53 +0200
Organization PointedEars Software (PES)
Message-ID <1876626.bB369e8A3T@PointedEars.de> (permalink)
References (2 earlier) <f4mddoF6nddU1@mid.individual.net> <22557651-daf7-4252-b3a4-c9df083be85c@googlegroups.com> <f4mqfvF9pi1U1@mid.individual.net> <526590a8-1952-4da6-a8cd-525eef79df6f@googlegroups.com> <f4rmr3Fdt6iU1@mid.individual.net>

Show all headers | View raw


Rolf Niepraschk wrote:

> Now I have found a solution by myself (old fashion syntax):
> 
> //=====================================================
> function foo() {
>    var fn_done, fn_each;
>    var ctx = {
>      done:function(fn) {fn_done = fn; return this;},
>      each:function(fn) {fn_each = fn; return this;}
>    }
>    setTimeout(function() {
>      var oldItems = [1,2,3,4],
>        newItems = oldItems.map(function(x) {return x * 2});
>      if (typeof fn_each == 'function') {
>        for (var i=0; i<oldItems.length; i++) {

Unless oldItems.length changes,

  for (var i=0, len = oldItems.length; i < len; i++) {

is more efficient.

>          fn_each(oldItems[i], newItems[i]);
>        }

This loop is pointlessly inefficient in any case.  You are already using 
Array.prototype.map(), thereby iterating through the array, so you can 
either output or accumulate the old and new value *in the callback*.

>      }
>      if (typeof fn_done == 'function') fn_done(newItems);
>    }, 500);
>    return ctx;
> }
> 
> console.log('------------------------------');
> foo()
> .each(function(oldItem, newItem) {
>    console.log('oldItem: ' + oldItem + '    ' + 'newItem: ' + newItem);
> })
> .done(function(result) {
>    console.log('------------------------------');
>    console.log('result: ' + result);
>    console.log('------------------------------');
> });
> //=====================================================
> 
> I am sure this can be more elegant but it seems to work.

In general that approach is correct.

However, in Node.js 4.8+ and other conforming implementations of ECMAScript 
Ed. 6+, it is not necessary anymore to write all that custom, little-tested 
code for implementing a well-known design pattern:

  var oldItems = Array.apply(null, {length: 10}).map(
    function (e, i) { return i; });

  var foo = function(oldItem, newItem) {
    console.log('oldItem: ' + oldItem + ',', 'newItem: ' + newItem);
  };

  new Promise(function (resolve, reject) {
    setTimeout(function() {
      var newItems = oldItems.map(function (item) {
        var newItem = item * 2;
        foo(item, newItem);
        return newItem;
      });

      resolve(newItems);
    }, 500);
  }).then(function (result) {
    /* Use window.alert() for testing in the Chromium console instead */
    console.log(result);
  });

It should be other other way around anyway: whatever the setTimeout() call 
is replaced with should provide a way to pass the success and failure
callback by implementing a Promise-like pattern, just like the original 
setTimeout *already* *does* (but only for success).  Code that can 
reasonably be abstracted away from the caller should be abstracted away.

For example, instances of XMLHTTPRequest() now have an “onload” and 
“onerror” property, and the new, even more compact Fetch API (not available 
in Node.js 4.8.2) returns Promises.

There is no best one-size-fits-all solution, and chaining is not everything.
Do not fall into the jQuery trap.

(Told you so.)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-17 12:39 +0200
  Re: Iterating through an array (special syntax) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-17 05:54 -0700
    Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-17 15:03 +0200
      Re: Iterating through an array (special syntax) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-17 06:19 -0700
        Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-17 18:46 +0200
          Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-18 13:55 +0200
            Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-18 14:25 +0200
              Re: Iterating through an array (special syntax) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-10-18 15:38 +0200
              Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-18 15:49 +0200
          Re: Iterating through an array (special syntax) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-18 16:25 -0700
            Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-19 07:50 +0200
            Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-19 15:15 +0200
              Re: Iterating through an array (special syntax) Julio Di Egidio <julio@diegidio.name> - 2017-10-19 06:36 -0700
                Re: Iterating through an array (special syntax) Julio Di Egidio <julio@diegidio.name> - 2017-10-19 06:39 -0700
                Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-19 18:16 +0200
                Re: Iterating through an array (special syntax) Julio Di Egidio <julio@diegidio.name> - 2017-10-19 18:55 -0700
              Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-19 21:53 +0200
                Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-20 07:55 +0200
                Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-20 21:48 +0200
  Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-18 04:50 +0200

csiph-web