如何使用映射函数访问嵌套数组中的键?

时间:2019-05-23 15:28:02

标签: javascript arrays google-apps-script ecmascript-5 map-function

我有一个嵌套数组:

var arr = [
{
"id": 18,
"published": 1,
"include_inproject": 1,
"created_at": "2017-09-21 11:27:46",
"updated_at": "2018-06-18 15:35:38",
"name": "1-1 Benban Arinna Solar PV II",
"status_id": 15,
"capacity": 20,
"technology_id": 2,
"commercial_operations": "2019-03-31 00:00:00",
"commercial_operations_estemated": "Q1 2019",
"commercial_operations_end": "",
"commercial_operations_end_estemated": "",
"start_construction": "2018-06-18 00:00:00",
"project_id": 530,
"start_construction_estimated": "June 2018",
"capacity_low": "",
"capacity_high": "",
"technology": "Photovoltaic (PV)",
"country": "Egypt",
"region": "North Africa",
"fuels": "Solar",
"status": "Planned",
"ownership_type": "IPP",
"Units": [
  {
    "id": 1,
    "published": 1,
    "include_inproject": 1,
    "created_at": "2017-09-21 12:14:47",
    "updated_at": "2017-09-21 12:14:47",
    "name": "1-1 Benban Arinna Solar PV II",
    "grid_connection": "On-grid",
    "status_id": 14,
    "capacity": 20,
    "technology_id": 1,
    "commercial_operations": "1959-01-01 00:00:00",
    "commercial_operations_estemated": "1959",
    "commercial_operations_end": null,
    "commercial_operations_end_estemated": "",
    "start_construction": null,
    "project_id": 470,
    "start_construction_estimated": "",
    "capacity_low": null,
    "capacity_high": null
      }
    ]
}
]

该数组包含多个元素,如上一个。

我想访问数组中的某些键,并且可以使用.map函数:

var cols1 = arr.map(function(obj){
return obj["name"]   
})

这将返回整个第一级数组中“名称”键下的所有值。

每个数组元素都有一个名为“ Units”的子数组,我也想访问该数组中的一个键,但是这样做很麻烦。我知道如何在地图之外导航到它:

arr [0] .Units [0] .name

但是这只会检索键值的第一个实例,我想映射整个数组并检索“ Units”数组中的所有值。

我尝试过:

arr[0].Units.map(function(obj){
return obj[name]
})

但是出现错误:无法调用未定义的方法“ map”

我认为问题在于当所有“ Units”子数组不在一个数组中时,它们会映射到它们。我只是不知道该如何解决。 任何帮助都会很棒!

4 个答案:

答案 0 :(得分:0)

此代码循环遍历每个对象单元,并将名称推入数组:

var arr = [{
  "id": 18,
  "published": 1,
  "include_inproject": 1,
  "created_at": "2017-09-21 11:27:46",
  "updated_at": "2018-06-18 15:35:38",
  "name": "1-1 Benban Arinna Solar PV II",
  "status_id": 15,
  "capacity": 20,
  "technology_id": 2,
  "commercial_operations": "2019-03-31 00:00:00",
  "commercial_operations_estemated": "Q1 2019",
  "commercial_operations_end": "",
  "commercial_operations_end_estemated": "",
  "start_construction": "2018-06-18 00:00:00",
  "project_id": 530,
  "start_construction_estimated": "June 2018",
  "capacity_low": "",
  "capacity_high": "",
  "technology": "Photovoltaic (PV)",
  "country": "Egypt",
  "region": "North Africa",
  "fuels": "Solar",
  "status": "Planned",
  "ownership_type": "IPP",
  "Units": [{
    "id": 1,
    "published": 1,
    "include_inproject": 1,
    "created_at": "2017-09-21 12:14:47",
    "updated_at": "2017-09-21 12:14:47",
    "name": "1-1 Benban Arinna Solar PV II",
    "grid_connection": "On-grid",
    "status_id": 14,
    "capacity": 20,
    "technology_id": 1,
    "commercial_operations": "1959-01-01 00:00:00",
    "commercial_operations_estemated": "1959",
    "commercial_operations_end": null,
    "commercial_operations_end_estemated": "",
    "start_construction": null,
    "project_id": 470,
    "start_construction_estimated": "",
    "capacity_low": null,
    "capacity_high": null
  }]
}]



var names = []
arr.forEach(obj => obj.Units.forEach(value => names.push(value.name)))


console.log(names)

答案 1 :(得分:0)

您可以使用forEachmap将创建一个新数组。因此,使用map将导致像

这样的嵌套数组
[
  ["1-1 Benban Arinna Solar PV II"]
]

var arr = [{
  "id": 18,
  "published": 1,
  "include_inproject": 1,
  "created_at": "2017-09-21 11:27:46",
  "updated_at": "2018-06-18 15:35:38",
  "name": "1-1 Benban Arinna Solar PV II",
  "status_id": 15,
  "capacity": 20,
  "technology_id": 2,
  "commercial_operations": "2019-03-31 00:00:00",
  "commercial_operations_estemated": "Q1 2019",
  "commercial_operations_end": "",
  "commercial_operations_end_estemated": "",
  "start_construction": "2018-06-18 00:00:00",
  "project_id": 530,
  "start_construction_estimated": "June 2018",
  "capacity_low": "",
  "capacity_high": "",
  "technology": "Photovoltaic (PV)",
  "country": "Egypt",
  "region": "North Africa",
  "fuels": "Solar",
  "status": "Planned",
  "ownership_type": "IPP",
  "Units": [{
    "id": 1,
    "published": 1,
    "include_inproject": 1,
    "created_at": "2017-09-21 12:14:47",
    "updated_at": "2017-09-21 12:14:47",
    "name": "1-1 Benban Arinna Solar PV II",
    "grid_connection": "On-grid",
    "status_id": 14,
    "capacity": 20,
    "technology_id": 1,
    "commercial_operations": "1959-01-01 00:00:00",
    "commercial_operations_estemated": "1959",
    "commercial_operations_end": null,
    "commercial_operations_end_estemated": "",
    "start_construction": null,
    "project_id": 470,
    "start_construction_estimated": "",
    "capacity_low": null,
    "capacity_high": null
  }]
}]
let arys = []
arr.forEach(function(item) {

  return item.Units.forEach(function(elem) {
    arys.push(elem.name)
  })

});

console.log(arys)

答案 2 :(得分:0)

您可以创建一个递归函数,以使所有名称都达到n级。下面的函数将返回对象数组所有级别的所有名称的数组。

尝试以下操作:

let arr = [ { "id": 18, "published": 1, "include_inproject": 1, "created_at": "2017-09-21 11:27:46", "updated_at": "2018-06-18 15:35:38", "name": "1-1 Benban Arinna Solar PV II", "status_id": 15, "capacity": 20, "technology_id": 2, "commercial_operations": "2019-03-31 00:00:00", "commercial_operations_estemated": "Q1 2019", "commercial_operations_end": "", "commercial_operations_end_estemated": "", "start_construction": "2018-06-18 00:00:00", "project_id": 530, "start_construction_estimated": "June 2018", "capacity_low": "", "capacity_high": "", "technology": "Photovoltaic (PV)", "country": "Egypt", "region": "North Africa", "fuels": "Solar", "status": "Planned", "ownership_type": "IPP", "Units": [ { "id": 1, "published": 1, "include_inproject": 1, "created_at": "2017-09-21 12:14:47", "updated_at": "2017-09-21 12:14:47", "name": "1-1 Benban Arinna Solar PV II", "grid_connection": "On-grid", "status_id": 14, "capacity": 20, "technology_id": 1, "commercial_operations": "1959-01-01 00:00:00", "commercial_operations_estemated": "1959", "commercial_operations_end": null, "commercial_operations_end_estemated": "", "start_construction": null, "project_id": 470, "start_construction_estimated": "", "capacity_low": null, "capacity_high": null } ] } ]

function getAllNames(arr){
  if(!arr)
    return [];
  let result = [];
  arr.forEach(({name, Units})=>{
    result.push(name);
    result = result.concat(getAllNames(Units));
  });
  return result;
}


console.log(getAllNames(arr));

答案 3 :(得分:0)

这是一种替代方法,使用JMESPath库对嵌套的JSON对象执行查询:

     UserTags
Real Estate Insurance
Corporate - Finance Company
Corporate - Energy / Utility / Commodities
Corporate - Non-Financial Other
Government Entity - Central Bank
Government Entity - Regulator
Government Entity - Municipality
Asset Bank
     UserTags                                        AssignedTags
Real Estate Insurance                                 Real Estate
Real Estate Insurance                                 Insurance
Corporate - Finance Company                           Corporate
Corporate - Energy / Utility / Commodities            Corporate
Corporate - Non-Financial Other                       Corporate
Government Entity - Central Bank                      Government
Government Entity - Central Bank                      Bank
Government Entity - Regulator                         Government
Government Entity - Municipality                      Government
Government Entity - Municipality                      Municipality
Asset Bank                                            Asset
Asset Bank                                            Bank

签出github repo for the javascript JMESPath implementation,您可以直接在您的Apps Script项目中使用。