0

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

1
  • 1
    If you want to return a number - return a number: return i.properties.result; Commented Oct 7, 2016 at 9:39

3 Answers 3

3

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0

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; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.