【问题标题】:using .map on an array in javascript在javascript中的数组上使用.map
【发布时间】:2017-02-16 06:23:31
【问题描述】:

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

有什么想法可以做到这一点吗?最后我的目的是确定anyresult == 1

【问题讨论】:

  • 如果你想返回一个数字 - 返回一个数字:return i.properties.result;

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


【解决方案1】:

为了得到你想要的输出,你只需要

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

【讨论】:

    【解决方案2】:

    我想生成一个新数组,它只是 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);
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-29
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        • 2021-01-15
        • 2015-12-13
        • 1970-01-01
        相关资源
        最近更新 更多