Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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 - Fatal编程技术网

Javascript 确保变量是数组

Javascript 确保变量是数组,javascript,Javascript,我想对可能是单个对象或数组的变量运行map()。但是,如果变量是一个抛出错误的对象,我就不能这样做 我尝试使用spread运算符确保变量变成数组,但显然这也不起作用: // items can either be an object or an array of objects renderedItems = [...items] .filter(item => !!item) .map((item, index) => _this.renderItem(item,

我想对可能是单个对象或数组的变量运行
map()。但是,如果变量是一个抛出错误的对象,我就不能这样做

我尝试使用spread运算符确保变量变成数组,但显然这也不起作用:

// items can either be an object or an array of objects
renderedItems = [...items]
    .filter(item => !!item)
    .map((item, index) => _this.renderItem(item, index));

如何确保正在执行
filter()
map()
的变量是数组?我听说
Array。isArray
还不完全受支持。

您可以使用
instanceof
检查变量是否为数组

if(variable instanceof Array){
  //variable is an Array
} else {
  //variable is not an Array
}
var数组=[1,2,3,4,5];
var str=“String”;
函数isArray(obj){
返回数组的obj实例;
}
log(“数组是一个数组:“+isArray(数组));

log(“str是一个数组:+isArray(str))
您可以使用
instanceof
检查变量是否为数组

if(variable instanceof Array){
  //variable is an Array
} else {
  //variable is not an Array
}
var数组=[1,2,3,4,5];
var str=“String”;
函数isArray(obj){
返回数组的obj实例;
}
log(“数组是一个数组:“+isArray(数组));

log(“str是一个数组:+isArray(str))
对象数组类似于?
数组。iArray
现在得到广泛支持:对象数组类似于?
数组。iArray
现在得到广泛支持: