Reputation: 407
[{
"circlemarker": [{
"type": "circle_marker"
}, {
"latlong": "abc"
}]
}, {
"connector_marker": [{
"type": "icon_marker"
}, {
"latlong": "pqr"
}]
}, {
"icon_marker": [{
"type": "connector_marker"
}, {
"latlong": "xyz"
}]
}]
I want to access latlong
values of each marker. So how can I have access to each property in this structure.
Upvotes: 2
Views: 88
Reputation: 75666
so for each object in the array, you want to pluck the latlong
from the first key which also references another array of objects. Man I would fix this data structure but if you can't control it, you can do this:
#!/usr/bin/env node
var data = [{
"circlemarker": [{
"type": "circle_marker"
}, {
"latlong": "abc"
}]
}, {
"connector_marker": [{
"type": "icon_marker"
}, {
"latlong": "pqr"
}]
}, {
"icon_marker": [{
"type": "connector_marker"
}, {
"latlong": "xyz"
}]
}];
var _ = require('lodash')
, coords = [];
_.each(data, function(item){
//console.log(item);
var key = _(Object.keys(item)).first()
, first = item[key]
, latLong = _.pluck(first, 'latlong')[1];
if ( latLong ) {
coords.push(latLong);
}
});
console.log(coords);
Produces the following output:
[ 'abc', 'pqr', 'xyz' ]
Upvotes: -1
Reputation: 292
You can get latlong data:
for (var a = 0; a < obj.length; a++) {
var key = Object.keys(obj[a])[0];
var latlong = obj[a][key][1];
console.log(latlong));
}
But i think that data have not correct structure, this is better solution:
var markers = [{
"name": "circlemarker",
"type": "circle_marker"
"latlong": "abc"
}, {
"name": "connector_marker",
"type": "icon_marker",
"latlong": "pqr"
}, {
"name": "icon_marker",
"type": "connector_marker",
"latlong": "xyz"
}];
Upvotes: 2
Reputation: 25882
I think this should work for you:-
var makers = [{"circlemarker":[{"type":"circle_marker"},{"latlong":"abc"}]},{"connector_marker":[{"type":"icon_marker"},{"latlong":"pqr"}]},{"icon_marker":[{"type":"connector_marker"},{"latlong":"xyz"}]}];
makers.forEach(function(maker){
var makerName = Object.keys(maker)[0];
console.log(maker[makerName][1]["latlong"]);
});
Upvotes: 1