Reputation: 1234
I'm an R programmer and think of arrays in javascript kind of like lists in R. In R when I want to access elements of a list I would use lapply()
and do.call
to apply a function to the inner elements and then place the result into a new data structure. I read about the map
function in javascript but I can't seems to crack it in my case:
data = {"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "CRS" } },
"features": [
{ "type": "Feature",
"id": 6,
"properties": {"species": "giraffe",
"result": 0,
"popup_text": "Some text" },
"geometry": { "type": "Point", "coordinates": [ 1, 5] } },
{ "type": "Feature",
"id": 7,
"properties": { "species": "pig",
"result": 0,
"popup_text": "Some text" },
"geometry": { "type": "Point", "coordinates": [ 2,3 ] } },
{ "type": "Feature",
"id": 8,
"properties": { "species": "goat",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 1,6 ] } },
{ "type": "Feature",
"id": 16,
"properties": { "species": "giraffe",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 3,4 ] } },
{ "type": "Feature",
"id": 18,
"properties": { "species": "pig",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 2,7 ] } }
]
}
So, data.features
is an array of 5 elements, each of these elements contains 4 arrays: type
, id
, properties
, geometry
. I would like to generate a new array that is just the properties.result
value. It would have a structure like this:
newdata = [0, 0, 0, 0, 0]
So far I tried the following but it doesn't result in what I want:
var result = data.features.map(function(i) {
return{
result: i.properties.result
};
});
console.log(result)
Any ideas how I can do this? In the end my purpose is to determine if any
of the result == 1
Upvotes: 0
Views: 70
Reputation: 68393
I would like to generate a new array that is just the properties.result value.
You were almost on the right path
var result = data.features.map(function(i) {
return i.properties.result;
});
console.log(result)
In the end my purpose is to determine if any of the result == 1
you can use filter
var hasAny1 = data.features.filter(function(i) {
return i.properties.result == 1;
}).length > 0;
console.log(hasAny1);
Or use some
var hasAny1 = data.features.some(function(i) {
return i.properties.result == 1;
});
console.log(hasAny1);
Upvotes: 1
Reputation: 136094
To get the output you wanted you just needed
var result = data.features.map(function(i) {
return i.properties.result;
});
That will result in an array of [0,0,0,0,0]
.
To determine if any of them are 1 you could use Array.some
var areAny1 = result.some(function(x) { return x == 1; });
Live example below
var data = {"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "CRS" } },
"features": [
{ "type": "Feature",
"id": 6,
"properties": {"species": "giraffe",
"result": 0,
"popup_text": "Some text" },
"geometry": { "type": "Point", "coordinates": [ 1, 5] } },
{ "type": "Feature",
"id": 7,
"properties": { "species": "pig",
"result": 0,
"popup_text": "Some text" },
"geometry": { "type": "Point", "coordinates": [ 2,3 ] } },
{ "type": "Feature",
"id": 8,
"properties": { "species": "goat",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 1,6 ] } },
{ "type": "Feature",
"id": 16,
"properties": { "species": "giraffe",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 3,4 ] } },
{ "type": "Feature",
"id": 18,
"properties": { "species": "pig",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 2,7 ] } }
]
};
var result = data.features.map(function(x){
return x.properties.result;
});
console.log(result);
var areAny1 = result.some(function(x) { return x === 1 });
console.log(areAny1);
Upvotes: 3
Reputation: 81
You should just return the value that you want to be an element of the new array:
data.features.map(function (x) {
return x.properties.result;
});
Upvotes: 0