Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查值是否在嵌套数组中的更好方法-Javascript_Javascript_Typescript_Google Maps_Google Places Api_Googleplacesautocomplete - Fatal编程技术网

检查值是否在嵌套数组中的更好方法-Javascript

检查值是否在嵌套数组中的更好方法-Javascript,javascript,typescript,google-maps,google-places-api,googleplacesautocomplete,Javascript,Typescript,Google Maps,Google Places Api,Googleplacesautocomplete,我正在使用GooglePlacesAPI。我注意到,当我进入机构时,并不总是有zipcode、country、city,如果有,它们也不总是在address_components数组中的相同索引中 现在,我试图找出它是否具有上面列出的任何内容,以及它是否返回值。有没有更好的方法来实现这一点: getPlaceTypeValue(addressComponents: Places[], type: string): string { let value = null; for (c

我正在使用GooglePlacesAPI。我注意到,当我进入机构时,并不总是有zipcode、country、city,如果有,它们也不总是在address_components数组中的相同索引中

现在,我试图找出它是否具有上面列出的任何内容,以及它是否返回值。有没有更好的方法来实现这一点:

getPlaceTypeValue(addressComponents: Places[], type: string): string {
    let value = null;
    for (const [i] of addressComponents.entries()) {
      if (addressComponents[i].types.includes(type)) {
        value = addressComponents[i].long_name;
        break;
      }
    }
    return value;
  } 
console.log(this.placesService.getPlaceTypeValue(
address.address_components, 'postal_code'));
//Returns 77500
数据

[
      {
        "long_name": "Hotel Zone",
        "short_name": "Hotel Zone",
        "types": [
          "sublocality_level_1",
          "sublocality",
          "political"
        ]
      },
      {
        "long_name": "Kukulcan Boulevard",
        "short_name": "Kukulcan Boulevard",
        "types": [
          "neighborhood",
          "political"
        ]
      },
      {
        "long_name": "Cancún",
        "short_name": "Cancún",
        "types": [
          "locality",
          "political"
        ]
      },
      {
        "long_name": "Quintana Roo",
        "short_name": "Q.R.",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Mexico",
        "short_name": "MX",
        "types": [
          "country",
          "political"
        ]
      },
      {
        "long_name": "77500",
        "short_name": "77500",
        "types": [
          "postal_code"
        ]
      }
    ]

首先,我要改变这一点:

for (const [i] of addressComponents.entries()) {
  if (addressComponents[i].types.includes(type)) {
为此:

for (const {types} of addressComponents) {
  if (types.includes(type)) {
或者,如果您喜欢功能更强大的方法,则在一行
return
语句中使用
Array#find

return (addressComponents.find(({types}) => types.includes(type)) || {}).long_name || null;
只有当返回值
未定义
对您不合适时,才需要
|null
部分

速度 如果在你的问题中“更好”意味着“更快”,那么就选择老派的
for
循环:

for (let i = 0, len = addressComponents.length; i < len; i++) {
  if (addressComponents[i].types.includes(type)) {
for(设i=0,len=addressComponents.length;i
首先,我要改变这一点:

for (const [i] of addressComponents.entries()) {
  if (addressComponents[i].types.includes(type)) {
为此:

for (const {types} of addressComponents) {
  if (types.includes(type)) {
或者,如果您喜欢功能更强大的方法,则在一行
return
语句中使用
Array#find

return (addressComponents.find(({types}) => types.includes(type)) || {}).long_name || null;
只有当返回值
未定义
对您不合适时,才需要
|null
部分

速度 如果在你的问题中“更好”意味着“更快”,那么就选择老派的
for
循环:

for (let i = 0, len = addressComponents.length; i < len; i++) {
  if (addressComponents[i].types.includes(type)) {
for(设i=0,len=addressComponents.length;i
Holy s**t很酷。你能给我解释一下这一部分吗
const{types}
现在哪个解决方案更快?
{types}
是解构的,很像
[i]
也是解构的,但它是从另一个迭代器进行的。你使用了
.entries()
,它实际上会给你两个[index,value],其中只包含索引。我将数组本身视为iterable,它只提供值(对象)。对于这些值,我使用分解结构来提取类型。如果您关心速度,那么没有什么比旧式的
for
循环更好(直到引擎更好地优化新的结构)在这种情况下,速度不是一个问题,因为数据集总是非常小的,我的意思是更聪明的方法,你的答案很好。我在JS上学到了新东西。我在这方面正确吗…
types.includes(type))|{}
如果为true,则返回一个对象,如果不返回
{}
,如果它返回
{}
,然后
{}。long\u name
将是
未定义的
,如果它的
未定义
,它将返回
null
是的,你答对了。有一个“gotcha”,如果
long\u name
是一个空字符串,那么您也会得到一个
null
。最好使用
undefined
作为函数的返回值,只需跳过
null
。太酷了。您能给我解释一下这部分
const{types}
吗?现在哪种解决方案更快?
{types}
是解构的,很像
[i]
也是解构的,但它是从另一个迭代器执行的。您使用了
.entries()
,它实际上为您提供了成对的[index,value],您只获取了其中的索引。我将数组本身作为iterable,它只提供了值(对象).对于那些我使用解构来删除类型。如果速度是你关心的问题,那么没有什么能比旧式的
for
循环更好(直到引擎更好地优化新的结构)在这种情况下,速度不是一个问题,因为数据集总是非常小的,我的意思是更聪明的方法,你的答案很好。我在JS上学到了新东西。我在这方面正确吗…
types.includes(type))|{}
如果为true,则返回一个对象,如果不返回
{}
,如果它返回
{}
,然后
{}。long\u name
将是
未定义的
,如果它的
未定义
,它将返回
null
是的,你答对了。有一个“gotcha”,如果
long\u name
是一个空字符串,那么您还将得到一个
null
。最好使用
undefined
作为函数的返回值,只需跳过该
null