简体   繁体   English

javascript测试长度

[英]javascript testing length

If I have an object MyObject that contains an array of objects ListOfOtherObjects and I write this: 如果我有一个MyObject对象,它包含对象ListOfOtherObjects的数组,则可以这样写:

   if (MyObject.ListOfOtherObjects.length !== 0) {...}

to test and see if the array contains OtherObjects, is it the same as writing this: 测试并查看数组是否包含OtherObjects,是否与编写此代码相同:

   if (MyObject.ListOfOtherObjects) {...}

Thanks. 谢谢。

Nope, you need to check the length property. 不,您需要检查length属性。 An Array is always truthy , like any other Object . Array永远是真实的 ,就像其他Object

You can, however, omit the explicit check of !== 0 . 但是,您可以省略!== 0的显式检查。

if (MyObject.ListOfOtherObjects.length) {...}

This condition will be true if the Array has at least one element. 如果Array具有至少一个元素,则此条件为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM