lodash:如何做不区分大小写的_.find()

时间:2017-10-04 23:24:43

标签: javascript lodash

我有一个复杂的数据结构,带有嵌套数组,我需要使用不区分大小写的搜索来查找带有字符串值的元素。

由于执行嵌套查询的简单语法,我开始使用lodash - 它运行良好,但搜索区分大小写。

我想找一个" address_components"元素"类型" '路线的价值'和" long_name"值等于名为' targetStreet'。

的参数

这是我到目前为止的lodash代码:

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

问题: 如何匹配" long_name"房产案例不敏感?

以下是“geocodeResult”的示例。数据结构(实际上是Google地理编码结果对象)。

请注意,此数据结构是动态的...... Google会返回最具体的结果,但如果找不到街道的匹配项,那么它就不会返回街道组件,例如"类型" " address_components"中的数组具有非确定性值。

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

3 个答案:

答案 0 :(得分:6)

在比较之前将两个值转换为相同的大小写。

var search = 'Australia'.toLowerCase();

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

答案 1 :(得分:2)

这是使用lodash _.find()函数搜索不区分大小写的方式

const propertyName = "Basic";
const findName = _.find(this.object, item => {
        return item.name.toLowerCase() === propertyName.toLowerCase();
    });

答案 2 :(得分:1)

您使用lodash.find的方式对应lodash.match简写(https://lodash.com/docs/4.17.4#find),遗憾的是,您无法通过regexp或函数在对象属性中获得不敏感的搜索。

您必须编写类似的内容来执行搜索(假设小写为targetStreet):

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

您也可以使用ES6语法+ Array.prototype.findArray.prototype.includes(对于最近的浏览器)在没有lodash的情况下编写

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