lodash: 如何在 _.find() 中忽略大小写?

4
我有一个复杂的数据结构,其中包含嵌套数组,我需要使用不区分大小写的搜索方法查找带有字符串值的元素。
由于lodash具有执行嵌套查询的简单语法,因此我已开始使用它 - 它很好用,但是搜索区分大小写。
我想要查找一个"address_components"元素,其"types"值为'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"
    ]
  }
]
3个回答

11

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

var search = 'Australia'.toLowerCase();

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

这对于给定的数据结构不起作用。您的函数将为集合中的每个对象运行一次。在我的示例中,有两个对象。这些对象反过来又有六个属性……其中我感兴趣的仅是“address_components”。然后在其中……我需要检查是否有一个带有值“route”的“types”数组(它可能不存在)……如果是这样,那么我需要检查“long_name”属性……所以您必须正确嵌套,并且我们不能依赖属性存在。 - JTech
2
好的,我正在回答“如何进行不区分大小写的_.find()”这个问题。 - fubar

3
您使用 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;
   });
});

您也可以使用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
   ));

3
这是使用lodash _.find()函数进行不区分大小写搜索的方法。
const propertyName = "Basic";
const findName = _.find(this.object, item => {
        return item.name.toLowerCase() === propertyName.toLowerCase();
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接