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


Groups > comp.lang.javascript > #26017

Re: Push object to array syntax

Newsgroups comp.lang.javascript
Date 2014-10-29 00:29 -0700
References <cd6e00bf-8cb1-462a-94c8-b3f2ca7583a5@googlegroups.com>
Message-ID <87c0dcb1-8313-4cfc-9013-e92bd0e5022c@googlegroups.com> (permalink)
Subject Re: Push object to array syntax
From lingmaaki@gmail.com

Show all headers | View raw


More about JavaScript Array

http://www.corelangs.com/js/basics/arrays.html

Rahul


On Saturday, July 26, 2014 3:34:17 AM UTC+5:30, ant...@maxwelllucas.com wrote:
> I'm struggling with the syntax when trying to create an array of objects.
> 
> 
> I have a JSON feed which has a boat load of data in a don't need so I'm just grabbing the relevant values and creating a new array (it's to be using in a leaflet.js map btw)
> 
> 
> The format I need is:
> 
> markers: { 
> marker-name-1:{"lat":32.0852999,"lng":34.7817676}, 
> marker-name-2:{"lat":30.0444196,"lng":31.2357116}
> }
> 
>  
> Using:
> 
> var newArray = [];
> 
>  feed = Datafeed.query(function (response){ 
>     
>  _.each(response, function(rows) {
>  
>  var latstr = rows.custom_fields._wp_geo_latitude;
>  var lngstr = rows.custom_fields._wp_geo_longitude;
>  var title = rows.title;
>  
>  var result = {};
>  result [title] = {lat : latstr , lng : lngstr};
>  newArray.push(result);
>  
>  });
>  });
> 
> 
> I get:
> 
> {"marker-name-1":{"lat":"32.0852999","lng":"34.7817676"}},
> {"marker-name-2":{"lat":"30.0444196","lng":"31.2357116"}}
> 
> They are all separate objects which won't work dammit!
> 
> Some help on how I can construct what I'm after would be much appreciated.
> 
> Thanks
> 
> Antony

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