[英]JavaScript: How to filter array by values nested in object 2 level deep
[英]javascript array - how to filter one nested level and get only that value
如何僅過濾[{"address_components":{"types"
至少包含另一個固定數組的 1 個值的元素,比如說 ['administrative_area_level_3','locality']
如果我應該過濾第一級屬性,這可行
const elementToCheck = ['locality', 'administrative_area_level_3']
let validresult = results.filter(item => item.types.some(x => elementToCheck.includes(x)))
但是如何過濾更深的屬性並僅獲取long_name
屬性( long_name
數組)
我的object的形狀是這樣的
[
{
"address_components": [
{
"long_name": "18",
"short_name": "18",
"types": [
"street_number"
]
},
{
"long_name": "Viale Spagna",
"short_name": "Viale Spagna",
"types": [
"route"
]
},
{
"long_name": "Torvaianica",
"short_name": "Torvaianica",
"types": [
"locality",
"political"
]
},
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00040",
"short_name": "00040",
"types": [
"postal_code"
]
}
],
"formatted_address": "Viale Spagna, 18, 00040 Torvaianica RM, Italia",
"geometry": {
"location": {
"lat": 41.6212358,
"lng": 12.4623144
},
"location_type": "ROOFTOP",
"viewport": {
"south": 41.6198868197085,
"west": 12.4609654197085,
"north": 41.6225847802915,
"east": 12.4636633802915
}
},
"place_id": "ChIJ6f7OCpaUJRMR8fDocBTV4zY",
"plus_code": {
"compound_code": "JFC6+FW Torvaianica RM, Italia",
"global_code": "8FHJJFC6+FW"
},
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "20",
"short_name": "20",
"types": [
"street_number"
]
},
{
"long_name": "SP601",
"short_name": "SP601",
"types": [
"route"
]
},
{
"long_name": "Torvaianica",
"short_name": "Torvaianica",
"types": [
"locality",
"political"
]
},
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00040",
"short_name": "00040",
"types": [
"postal_code"
]
}
],
"formatted_address": "SP601, 20, 00040 Torvaianica RM, Italia",
"geometry": {
"location": {
"lat": 41.621202,
"lng": 12.462175
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"south": 41.61985301970849,
"west": 12.4608260197085,
"north": 41.62255098029149,
"east": 12.4635239802915
}
},
"place_id": "EiZTUDYwMSwgMjAsIDAwMDQwIFRvcnZhaWFuaWNhIFJNLCBJdGFseSIaEhgKFAoSCS8g0Q6WlCUTEU0Uj_evCCI-EBQ",
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "39-3",
"short_name": "39-3",
"types": [
"street_number"
]
},
{
"long_name": "Viale Spagna",
"short_name": "SP601",
"types": [
"route"
]
},
{
"long_name": "Torvaianica",
"short_name": "Torvaianica",
"types": [
"locality",
"political"
]
},
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00040",
"short_name": "00040",
"types": [
"postal_code"
]
}
],
"formatted_address": "SP601, 39-3, 00040 Torvaianica RM, Italia",
"geometry": {
"bounds": {
"south": 41.6210184,
"west": 12.461352,
"north": 41.6219781,
"east": 12.4623698
},
"location": {
"lat": 41.6214983,
"lng": 12.4618609
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"south": 41.6201492697085,
"west": 12.4605119197085,
"north": 41.6228472302915,
"east": 12.4632098802915
}
},
"place_id": "ChIJLyDRDpaUJRMRTBSP968IIj4",
"types": [
"route"
]
},
{
"address_components": [
{
"long_name": "Torvaianica",
"short_name": "Torvaianica",
"types": [
"locality",
"political"
]
},
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00071",
"short_name": "00071",
"types": [
"postal_code"
]
}
],
"formatted_address": "00071 Torvaianica RM, Italia",
"geometry": {
"bounds": {
"south": 41.5957908,
"west": 12.4262658,
"north": 41.6494953,
"east": 12.4877753
},
"location": {
"lat": 41.6208388,
"lng": 12.4626387
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 41.5957908,
"west": 12.4262658,
"north": 41.6494953,
"east": 12.4877753
}
},
"place_id": "ChIJX7p_spWUJRMRFDbxSJKxZKM",
"types": [
"locality",
"political"
]
},
{
"address_components": [
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00071",
"short_name": "00071",
"types": [
"postal_code"
]
}
],
"formatted_address": "00071 Pomezia RM, Italia",
"geometry": {
"bounds": {
"south": 41.5957908,
"west": 12.4158118,
"north": 41.7145057,
"east": 12.5952973
},
"location": {
"lat": 41.6697279,
"lng": 12.5016822
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 41.5957908,
"west": 12.4158118,
"north": 41.7145057,
"east": 12.5952973
}
},
"place_id": "ChIJlWQgGkmSJRMRD7RApgRHQOo",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"address_components": [
{
"long_name": "00071",
"short_name": "00071",
"types": [
"postal_code"
]
},
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
}
],
"formatted_address": "00071 Pomezia RM, Italia",
"geometry": {
"bounds": {
"south": 41.5957908,
"west": 12.4156635,
"north": 41.7145057,
"east": 12.5952973
},
"location": {
"lat": 41.6556868,
"lng": 12.5000093
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 41.5957908,
"west": 12.4156635,
"north": 41.7145057,
"east": 12.5952973
}
},
"place_id": "ChIJXWzlDbyTJRMRzo9qlicXv3w",
"types": [
"postal_code"
]
},
{
"address_components": [
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Città Metropolitana di Roma, Italia",
"geometry": {
"bounds": {
"south": 41.407571,
"west": 11.7335442,
"north": 42.2960412,
"east": 13.2962754
},
"location": {
"lat": 41.9027008,
"lng": 12.4962352
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 41.407571,
"west": 11.7335442,
"north": 42.2960412,
"east": 13.2962754
}
},
"place_id": "ChIJw0rXGxGKJRMRAIE4sppPCQM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"address_components": [
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Lazio, Italia",
"geometry": {
"bounds": {
"south": 40.7847377,
"west": 11.4493842,
"north": 42.8387207,
"east": 14.0276424
},
"location": {
"lat": 41.6552418,
"lng": 12.989615
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 40.7847377,
"west": 11.4493842,
"north": 42.8387207,
"east": 14.0276424
}
},
"place_id": "ChIJNWU6NebuJBMRKYWj8WSQSm8",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"address_components": [
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Italia",
"geometry": {
"bounds": {
"south": 35.4897,
"west": 6.6267201,
"north": 47.092,
"east": 18.7975999
},
"location": {
"lat": 41.87194,
"lng": 12.56738
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 35.4897,
"west": 6.6267201,
"north": 47.092,
"east": 18.7975999
}
},
"place_id": "ChIJA9KNRIL-1BIRb15jJFz1LOI",
"types": [
"country",
"political"
]
},
{
"address_components": [
{
"long_name": "JFC6+FW",
"short_name": "JFC6+FW",
"types": [
"plus_code"
]
},
{
"long_name": "Torvaianica",
"short_name": "Torvaianica",
"types": [
"locality",
"political"
]
},
{
"long_name": "Pomezia",
"short_name": "Pomezia",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Città Metropolitana di Roma",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00071",
"short_name": "00071",
"types": [
"postal_code"
]
}
],
"formatted_address": "JFC6+FW Torvaianica RM, Italia",
"geometry": {
"bounds": {
"south": 41.621125,
"west": 12.46225,
"north": 41.62125,
"east": 12.462375
},
"location": {
"lat": 41.6212353,
"lng": 12.4623016
},
"location_type": "ROOFTOP",
"viewport": {
"south": 41.61983851970851,
"west": 12.4609635197085,
"north": 41.62253648029151,
"east": 12.4636614802915
}
},
"place_id": "GhIJdE9oo4TPREAR5saZy7LsKEA",
"plus_code": {
"compound_code": "JFC6+FW Torvaianica RM, Italia",
"global_code": "8FHJJFC6+FW"
},
"types": [
"plus_code"
]
}
]
使用map()
遍歷頂層數組。 其中,使用address_components
屬性上的filter()
到 select 包含其中一個值的對象,並使用map()
來獲取它們的long_name
屬性。
let validresult = results.map(
result => result.address_components.filter(item.types.some(x => elementToCheck.includes(x)).map(item => item.long_name)
);
這將返回一個長名稱的二維數組。 如果要將它們合並到單個數組中,請使用results.flatMap()
而不是results.map
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.