如果数组还不存在,如何创建数组? [英] How to create an array if an array does not exist yet?

查看:54
本文介绍了如果数组还不存在,如何创建数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果尚不存在该如何创建数组?换句话说,如何将变量默认为空数组?

How do I create an array if it does not exist yet? In other words how to default a variable to an empty array?

推荐答案

如果要检查数组x是否存在并创建它(如果不存在),可以这样做

If you want to check whether an array x exists and create it if it doesn't, you can do

x = ( typeof x != 'undefined' && x instanceof Array ) ? x : []

这篇关于如果数组还不存在,如何创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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