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


Groups > comp.lang.javascript > #25567

Re: Push object to array syntax

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Push object to array syntax
Date 2014-07-26 11:08 +0200
Organization PointedEars Software (PES)
Message-ID <1615342.4NPermSF73@PointedEars.de> (permalink)
References <cd6e00bf-8cb1-462a-94c8-b3f2ca7583a5@googlegroups.com> <array-20140726001950@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

> antony@maxwelllucas.com writes:
>> newArray.push(result);
>> {"marker-name-1":{"lat":"32.0852999","lng":"34.7817676"}},
>> {"marker-name-2":{"lat":"30.0444196","lng":"31.2357116"}}
> 
>   Wild guess: something like:
> 
> for( var name in result )if( result.hasOwnProperty( name ))
> { newArray[ name ]= result[ name ]; break; }
> 
>   ?

GIGO.  This code will add the first own enumerable property of the object 
referred to by “result” to the object referred to by “newArray”, or 
overwrite an existing one, and then exit.  But the former object was created 
by the OP’s code just two line before that, and one line before the only 
relevant property has been added; so the property name is *known* and the 
error-prone, though not completely cluless, iteration-assignment is 
unnecessary.

This problem is, in essence, so simple that it does not excuse wild guesses.  
Please test before you post.  Nobody is helped by posting half-knowledge.

JavaScript/ECMAScript is not like PHP, for example.  There are no built-in 
associative arrays.  Using the bracket property accessor syntax LHS does not 
add an array element per se (only for particular numeric property names).  
Using Array instances (as created with the Array initialiser, “[]”) when you 
do not need Array properties – *numeric* indexes aso. – is wrong.

The following could be the solution the OP is looking for:

  /* we only need an Object instance here */
  var o = {
    /* create _data_ object (ES 5+) */
    markers: Object.create(null)
  };

  var feed = Datafeed.query(function (response) { 
    // …

    _.each(response, function (rows) {
      // …

      var markers = o.markers;

      /*
       * sanitize title here so that the object is not borked by a marker
       */

      /*
       * ES 3+; use markers[title] _here_ for more backwards compatibility
       */
      if (title in markers)
      {
        /*
         * or you could make the property value a reference to
         * an Array instance here, accumulating data for same-named markers
         */
        console.warn('Ignored duplicate marker: "' + title + '"');
      }
      else
      {
        markers[title] = {lat: latstr, lng: lngstr}; 
      }
    });
  });

The Object instance having a put() method that does the sanitizing and 
checks would even be cleaner.  jsx.map.Map() could be extended, or a Map
Proxy (ES 6) could be used directly.

Unfortunately, the OP has not read the FAQ on how to ask questions the smart 
way, so one cannot be sure as to the solution.  Also, them using the borken 
Google Groups instead of a proper newsreader, it is not likely that many 
knowledgable people will read their posting.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
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

Push object to array syntax antony@maxwelllucas.com - 2014-07-25 15:04 -0700
  Re: Push object to array syntax Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-26 11:08 +0200
  Re: Push object to array syntax Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-27 10:52 -0700
    Re: Push object to array syntax Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-27 18:24 -0700
      Re: Push object to array syntax antony@maxwelllucas.com - 2014-07-28 07:06 -0700
  Re: Push object to array syntax lingmaaki@gmail.com - 2014-10-29 00:29 -0700
    Re: Push object to array syntax John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-29 15:23 +0000
      Re: Push object to array syntax "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-29 17:34 +0100

csiph-web