Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25574
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2014-07-27 10:52 -0700 |
| References | <cd6e00bf-8cb1-462a-94c8-b3f2ca7583a5@googlegroups.com> |
| Message-ID | <e46aa28a-57b3-4bb6-9fec-e74c1680f670@googlegroups.com> (permalink) |
| Subject | Re: Push object to array syntax |
| From | Scott Sauyet <scott.sauyet@gmail.com> |
antony@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)
It's all right to reduce the data, but you really should show us
the structure of the data you receive. I'm making a guess and
assuming that the callback to Datafeed.query receives an array of
objects, but your sample code (assuming that the `_` is something
like Underscore or LoDash) would work with certain other structures
as well, in which case, my suggestion might not help.
> The format I need is:
> markers: {
> marker-name-1:{"lat":32.0852999,"lng":34.7817676},
> marker-name-2:{"lat":30.0444196,"lng":31.2357116}
> }
I assume you need an object wrapping that up:
{
markers: {
marker-name-1:{"lat":32.0852999,"lng":34.7817676},
marker-name-2:{"lat":30.0444196,"lng":31.2357116}
}
}
If not, I'm not quite sure what you mean.
This code might do what you
var newArray = Datafeed.query(function(response) {
return {markers: response.map(function(rows) {
var obj = {};
obj[rows.title] = {
lat: +rows.custom_fields._wp_geo_latitude,
lng: +rows.custom_fields._wp_geo_longitude
};
return obj;
})};
});
This is making the assumption that `Datafeed.query` returns
to you the result of your callback function. If not, you
would have to structure it slightly differently:
var newArray;
Datafeed.query(function(response) {
newArray = {markers: response.map(function(rows) {
var obj = {};
// ...
And if you need this code to run in environments that don't support
`Array.prototype.map`, you would need either to replace the `map`
call with a library `map` or shim it. A reasonable shim is found
on MDN [1]. A `map` from Underscore or LoDash would be used like
return {markers: _.map(response, function(rows) {
/* ... */
});
The `map` in the Ramda [2] library I've been working on puts the
list after the function, so would work like this:
return {markers: R.map(function(rows) {
/* ... */
}, response);
[1]: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Polyfill>
[2]: <https://github.com/CrossEye/ramda>
HTH,
-- Scott
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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