2017-10-04 53 views
1

我有一個複雜的數據結構,嵌套數組,我需要使用不區分大小寫的搜索來查找帶有字符串值的元素。lodash:如何做不區分大小寫_.find()

由於簡單的語法來執行嵌套查詢,我已經開始使用lodash - 它工作的很好,但搜索區分大小寫。

我想找到一個「address_components」元素,其中一個「類型」值爲'route'和'long_name'的值等於名爲'targetStreet'的參數。

這裏是lodash代碼,我到目前爲止有:

var result = _.find(geocodeResult, 
     { 
      'address_components': [ 
       { 
        types: ['route'], 
        'long_name': targetStreet 
       } 
      ] 
     }); 

問題: 我怎樣才能使「LONG_NAME」財產案件的匹配不敏感?

下面是一個'geocodeResult'數據結構(它實際上是Google Geocoding Result對象)的示例。

請注意,此數據結構是動態的... Google會返回最具體的結果,但如果它找不到街道匹配,那麼它將不會返回街道組件,例如「address_components」中的「types」數組具有非確定性值。

[ 
    { 
    "address_components": [ 
     { 
     "long_name": "V&a Lane", 
     "short_name": "V&a Ln", 
     "types": [ 
      "route" 
     ] 
     }, 
     { 
     "long_name": "Coonawarra", 
     "short_name": "Coonawarra", 
     "types": [ 
      "locality", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "Wattle Range Council", 
     "short_name": "Wattle Range", 
     "types": [ 
      "administrative_area_level_2", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "South Australia", 
     "short_name": "SA", 
     "types": [ 
      "administrative_area_level_1", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "Australia", 
     "short_name": "AU", 
     "types": [ 
      "country", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "5263", 
     "short_name": "5263", 
     "types": [ 
      "postal_code" 
     ] 
     } 
    ], 
    "formatted_address": "V&a Ln, Coonawarra SA 5263, Australia", 
    "geometry": { 
     "bounds": { 
     "south": -37.3238134, 
     "west": 140.8154452, 
     "north": -37.3228868, 
     "east": 140.97295129999998 
     }, 
     "location": { 
     "lat": -37.3233904, 
     "lng": 140.89510410000003 
     }, 
     "location_type": "GEOMETRIC_CENTER", 
     "viewport": { 
     "south": -37.3246990802915, 
     "west": 140.8154452, 
     "north": -37.3220011197085, 
     "east": 140.97295129999998 
     } 
    }, 
    "partial_match": true, 
    "place_id": "EiVWJmEgTG4sIENvb25hd2FycmEgU0EgNTI2MywgQXVzdHJhbGlh", 
    "types": [ 
     "route" 
    ] 
    }, 
    { 
    "address_components": [ 
     { 
     "long_name": "V&a Lane", 
     "short_name": "V&a Ln", 
     "types": [ 
      "route" 
     ] 
     }, 
     { 
     "long_name": "Coonawarra", 
     "short_name": "Coonawarra", 
     "types": [ 
      "locality", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "Wattle Range Council", 
     "short_name": "Wattle Range", 
     "types": [ 
      "administrative_area_level_2", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "South Australia", 
     "short_name": "SA", 
     "types": [ 
      "administrative_area_level_1", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "Australia", 
     "short_name": "AU", 
     "types": [ 
      "country", 
      "political" 
     ] 
     }, 
     { 
     "long_name": "5263", 
     "short_name": "5263", 
     "types": [ 
      "postal_code" 
     ] 
     } 
    ], 
    "formatted_address": "V&a Ln, Coonawarra SA 5263, Australia", 
    "geometry": { 
     "bounds": { 
     "south": -37.3238134, 
     "west": 140.8154452, 
     "north": -37.3228868, 
     "east": 140.97295129999998 
     }, 
     "location": { 
     "lat": -37.3233904, 
     "lng": 140.89510410000003 
     }, 
     "location_type": "GEOMETRIC_CENTER", 
     "viewport": { 
     "south": -37.3246990802915, 
     "west": 140.8154452, 
     "north": -37.3220011197085, 
     "east": 140.97295129999998 
     } 
    }, 
    "partial_match": true, 
    "place_id": "EiVWJmEgTG4sIENvb25hd2FycmEgU0EgNTI2MywgQXVzdHJhbGlh", 
    "types": [ 
     "route" 
    ] 
    } 
] 

回答

1

使用lodash.find的方式對應於lodash.match速記(https://lodash.com/docs/4.17.4#find),不幸的是你不能傳遞正則表達式或函數來獲取你的對象屬性不敏感的搜索。

你必須寫類似的東西來進行搜索(小寫假設targetStreet):

var result = _.find(geoCodeResult, function (result) { 
    return _.find(result.address_components, function(component) { 
     return _.includes(component.types, 'route') 
     && component.long_name.toLowerCase() === targetStreet; 
    }); 
}); 

你也可以把它寫不lodash,使用ES6語法+ Array.prototype.findArray.prototype.includes(對於最新的瀏覽器)

const result = geoCodeResult.find(result => 
    result.address_components.find(({ types, long_name }) => 
     types.includes('route') && long_name.toLowerCase() === targetStreet 
    )); 
2

在比較之前將這兩個值轉換爲相同的大小寫。

var search = 'Australia'.toLowerCase(); 

var result = _.find(geoCodeResult, function (location) { 
    return location.long_name.toLowerCase() === search; 
}); 
+0

這不適用於給定的數據結構。您的函數將爲集合中的每個對象運行一次。在我的例子中,有兩個。這些對象又有六個屬性......我唯一感興趣的是「address_components」。然後在那裏...我需要檢查是否有一個值爲「路由」(它可能不在那裏)的「類型」數組...如果是這樣,那麼我需要檢查「long_name」屬性。所以你必須得到嵌套的權利,我們不能依靠那裏的財產。 – JTech

+0

夠公平的。我回答了'如何做不區分大小寫_.find()'的問題。 – fubar