首页 > 解决方案 > javascript数组 - 如何过滤一个嵌套级别并仅获取该值

问题描述

如何仅过滤[{"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

我的对象的形状是这样的

[
    {
        "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"
        ]
    }
]

标签: javascriptarraysmultidimensional-arrayecmascript-6array-filter

解决方案


用于map()迭代顶级数组。在那里,使用filter()属性address_components来选择包含其中一个值的对象,并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


推荐阅读