是否可以在 javascript 中检查内联空值? [英] Is it possible to check for null inline in javascript?

查看:34
本文介绍了是否可以在 javascript 中检查内联空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以解析 Google Maps API JSON 的地址组件,然后返回城市/地点/路线名称.

如果 getAddressComponent() 找不到键,则返回 null.

let route = getAddressComponent(addressComponents, 'route').value.long_name;

所以假设它没有找到密钥,然后我得到一个 Error: TypeError: Cannot read property 'long_name' of undefined 显然是因为它没有定义.

除了条件方法(a === null)之外,我如何在javascript中检查null?

我怎样才能用 简单地像这样检查?

安全导航操作员

let route = getAddressComponent(addressComponents, 'route')?.value.long_name;

如果它不存在,它可能会将 route 设置为 null 而不是抛出错误?

解决方案

2020 Answer, It Exists!!!

您现在可以直接使用 ?. 内联来测试是否存在.它被称为 Optional Chaining Operator,所有现代浏览器都支持.

如果属性存在,则进行下一次检查,或返回值.任何失败都会立即短路并返回undefined.

const 示例 = {a: [first", {b:3}, false]}示例?.a//[第一",{b:3},假]示例?.b//未定义例如?.a?.[0]//第一"例如?.a?.[1]?.a//未定义例如?.a?.[1]?.b//3domElement?.parentElement?.children?.[3]?.nextElementSibling

为确保默认定义值,您可以使用 ??.如果需要第一个真值,可以使用 ||.

example?.c ??c"//"c";例如?.c ||c"//"c";例如?.a?.[2]??2//假例如?.a?.[2]||2//2

如果不勾选case,则left-side属性必须存在.否则会抛出异常.

example?.First//未定义示例?.First.Second//未捕获的类型错误:无法读取未定义的属性 'Second'

?. 浏览器支持 - 92%,12 月2021

?? 浏览器支持 - 92%

节点支持 - v14+

I have a function which parses the address components of the Google Maps API JSON and then returns the city / locality / route name.

The getAddressComponent() returns a null if it cannot find the key.

let route = getAddressComponent(addressComponents, 'route').value.long_name;

So let's say it didn't find the key, then I get a Error: TypeError: Cannot read property 'long_name' of undefined obviously because it's not defined.

How do I check for null in javascript other than the conditional method (a === null)?

How can I simply check like this with ?

EDIT : Safe Navigation Operator

let route = getAddressComponent(addressComponents, 'route')?.value.long_name;

And if it doesn't exists, it could probably set route to null instead of throwing a Error ?

解决方案

2020 Answer, It Exists!!!

You can now directly use ?. inline to test for existence. It is called the Optional Chaining Operator, supported by all modern browsers.

If a property exists, it proceeds to the next check, or returns the value. Any failure will immediately short-circuit and return undefined.

const example = {a: ["first", {b:3}, false]}

example?.a  // ["first", {b:3}, false]
example?.b  // undefined

example?.a?.[0]     // "first"
example?.a?.[1]?.a  // undefined
example?.a?.[1]?.b  // 3

domElement?.parentElement?.children?.[3]?.nextElementSibling

To ensure a default defined value, you can use ??. If you require the first truthy value, you can use ||.

example?.c ?? "c"  // "c"
example?.c || "c"  // "c"

example?.a?.[2] ?? 2  // false
example?.a?.[2] || 2  // 2

If you do not check a case, the left-side property must exist. If not, it will throw an exception.

example?.First         // undefined
example?.First.Second  // Uncaught TypeError: Cannot read property 'Second' of undefined

?. Browser Support - 92%, Dec 2021

?? Browser Support - 92%

Node Support - v14+

这篇关于是否可以在 javascript 中检查内联空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆