Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

javascript - 检查数组是否为空或不存在。 JS [重复](Check if array is empty or does not exist. JS [duplicate])

This question already has an answer here:(这个问题在这里已有答案:)

How to check if an array exist, if not create it, in JavaScript?(如何在JavaScript中检查数组是否存在(如果没有创建)?) 7 answers(7个答案)

What's the best way to check if an array is empty or doesn't exist?(检查数组是空还是不存在的最佳方法是什么?)

Something like this?(像这样的东西?) if(array.length < 1 || array == undefined){ //empty }   ask by Daft translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You want to do the check for undefined first.(您想先检查undefined 。)

If you do it the other way round, it will generate an error if the array is undefined.(如果你反过来这样做,如果数组是未定义的,它将产生一个错误。) if (array === undefined || array.length == 0) { // array empty or does not exist } Update(更新) This answer is getting a fair amount of attention, so I'd like to point out that my original answer, more than anything else, addressed the wrong order of the conditions being evaluated in the question.(这个答案得到了相当多的关注,所以我想指出,我的原始答案最重要的是解决了问题中评估条件的错误顺序。) In this sense, it fails to address several scenarios, such as null values, other types of objects with a length property, etc. It is also not very idiomatic JavaScript.(从这个意义上说,它无法解决几个场景,例如null值,具有length属性的其他类型的对象等。它也不是非常惯用的JavaScript。) The foolproof approach(万无一失的方法)
Taking some inspiration from the comments, below is what I currently consider to be the foolproof way to check whether an array is empty or does not exist.(从评论中获取一些灵感,下面是我目前认为是检查阵列是空的还是不存在的万无一失的方法。) It also takes into account that the variable might not refer to an array, but to some other type of object with a length property.(它还考虑到变量可能不引用数组,而是引用具有length属性的其他类型的对象。) if (!Array.isArray(array) || !array.length) { // array does not exist, is not an array, or is empty // ? do not attempt to process array } To break it down:(要打破它:) Array.isArray() , unsurprisingly, checks whether its argument is an array.(Array.isArray()Array.isArray()检查它的参数是否是一个数组。) This weeds out values like null , undefined and anything else that is not an array.(这会清除像nullundefined和其他任何不是数组的值。)
Note that this will also eliminate array-like objects , such as the arguments object and DOM NodeList objects.(请注意,这也将消除类似数组的对象 ,例如arguments对象和DOM NodeList对象。) Depending on your situation, this might not be the behavior you're after.(根据您的情况,这可能不是您所追求的行为。) The array.length condition checks whether the variable's length property evaluates to a truthy value.(array.length条件检查变量的length属性是否计算为truthy值。) Because the previous condition already established that we are indeed dealing with an array, more strict comparisons like array.length != 0 or array.length !== 0 are not required here.(因为先前的条件已经确定我们确实在处理数组,所以这里不需要更严格的比较,例如array.length != 0array.length !== 0 。) The pragmatic approach(务实的做法)
In a lot of cases, the above might seem like overkill.(在很多情况下,上述情况可能看起来有点过分。) Maybe you're using a higher order language like TypeScript that does most of the type-checking for you at compile-time, or you really don't care whether the object is actually an array, or just array-like.(也许您正在使用像TypeScript这样的高阶语言,它在编译时为您执行大部分类型检查,或者您实际上并不关心对象实际上是数组还是数组类。) In those cases, I tend to go for the following, more idiomatic JavaScript:(在这些情况下,我倾向于使用以下更惯用的JavaScript:) if (!array || !array.length) { // array or array.length are falsy // ? do not attempt to process array } Or, more frequently, its inverse:(或者,更常见的是它的逆:) if (array && array.length) { // array and array.length are truthy // ? probably OK to process array }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...