More efficient way to extract address components

前端 未结 9 1165
再見小時候
再見小時候 2020-12-15 10:23

Currenty, I\'m using the following code to get the country, postal code, locality and sub-locality:

var country, postal_code, locality, sublocality;
for (i =         


        
9条回答
  •  没有蜡笔的小新
    2020-12-15 10:49

    I made function before that extracts a list of values given a list of place types:

    const getValue = function(data, types=[]){
    /* used by results taken from Geocoder.geocode api */
    const values = data.reduce((values, address) => {
        return address.address_components.reduce((values2, component) => {
            if(component.types.reduce((result, type) => result || types.indexOf(type) > -1, false))
                values2.push(component.long_name);
            return values2
        }, []);
    
        if(buff.length)
            return [...values, ...buff];
        return values;
    }, []).filter(
        (value, index, self) => {
            return self.indexOf(value) === index;
        }
    );
    

    }

提交回复
热议问题