在javascript

时间:2016-10-07 09:36:13

标签: javascript multidimensional-array array.prototype.map

我是一名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

3 个答案:

答案 0 :(得分: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);




答案 1 :(得分:0)

  

我想生成一个新的数组   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);

或使用some

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

答案 2 :(得分:0)

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

data.features.map(function (x) { 
    return x.properties.result; 
});