0

我是一名 R 程序员,我认为 javascript 中的数组类似于 R 中的列表。在 R 中,当我想访问列表的元素时,我会使用lapply()并将do.call函数应用于内部元素,然后将结果放入新的数据结构。我在 javascript 中阅读了有关该map功能的信息,但在我的情况下似乎无法破解它:

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 ] } }
    ]
       }

所以,data.features是一个由 5 个元素组成的数组,每个元素都包含 4 个数组:typeidpropertiesgeometry。我想生成一个只是properties.result值的新数组。它会有这样的结构:

newdata = [0, 0, 0, 0, 0]

到目前为止,我尝试了以下方法,但没有得到我想要的结果:

var result = data.features.map(function(i) {
    return{
    result: i.properties.result
    };
});
console.log(result)

任何想法我怎么能做到这一点?最后我的目的是确定any是否result == 1

4

3 回答 3

3

要获得您想要的输出,您只需要

var result = data.features.map(function(i) {
    return i.properties.result;    
});

这将产生一个数组[0,0,0,0,0]

要确定它们中的任何一个是否为 1,您可以使用Array.some

var areAny1 = result.some(function(x) { return x == 1; });

下面的实时示例

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

于 2016-10-07T09:40:30.713 回答
1

我想生成一个只是 properties.result 值的新数组。

你几乎是在正确的道路上

var result = data.features.map(function(i) {
    return i.properties.result;
});
console.log(result)

最后我的目的是确定是否有任何结果 == 1

您可以使用filter

var hasAny1 = data.features.filter(function(i) {
    return i.properties.result == 1;
}).length > 0;
console.log(hasAny1);

或者使用一些

var hasAny1 = data.features.some(function(i) {
    return i.properties.result == 1;
});
console.log(hasAny1);
于 2016-10-07T09:40:07.230 回答
0

您应该只返回要成为新数组元素的值:

data.features.map(function (x) { 
    return x.properties.result; 
});
于 2016-10-07T09:40:19.993 回答