当页面第一次加载时,我需要检查image_array
中是否有图像并加载最后一张图像。
否则,我禁用预览按钮,提醒用户按下新图像按钮并创建一个空数组来放置图像;
问题是image_array
的else
一直在火上浇油。如果存在一个数组 - 它只是覆盖它,但警报不起作用。
if(image_array.length > 0)
$('#images').append('<img src="'+image_array[image_array.length-1]+'" class="images" id="1" />');
else{
$('#prev_image').attr('disabled', 'true');
$('#next_image').attr('disabled', 'true');
alert('Please get new image');
var image_array = [];
}
更新在加载html之前,我有这样的事情:
<?php if(count($images) != 0): ?>
<script type="text/javascript">
<?php echo "image_array = ".json_encode($images);?>
</script>
<?php endif; ?>
if (typeof image_array !== 'undefined' && image_array.length > 0) {
// the array is defined and has at least one element
}
由于隐式全局变量和变量提升的混合,您的问题可能正在发生。确保在声明变量时使用var
:
<?php echo "var image_array = ".json_encode($images);?>
// add var ^^^ here
然后确保您以后从未意外地重新声明该变量:
else {
...
image_array = []; // no var here
}
对于我来说,当我将它们放入jsfiddle时,肯定会有一些高评价的答案“有效”,但是当我有一个动态生成的数组列表时,答案中的很多代码对我来说都不起作用。
这就是IS为我工作的原因。
var from = [];
if(typeof from[0] !== undefined) {
//...
}
请注意,未定义周围没有引号,我不打扰长度。
我在Javascript中遇到了很多这个问题。对我来说,最好的方法是在检查长度之前进行非常广泛的检查。我在本问答中看到了其他一些解决方案,但我希望能够检查null
或undefined
或任何其他错误值。
if(!array || array.length == 0){
console.log("Array is either empty or does not exist")
}
这将首先检查undefined
,null
或其他错误值。如果其中任何一个为真,它将完成布尔值,因为这是一个OR
。然后可以检查更高风险的array.length
检查,如果数组未定义则可能会出错。如果array
是undefined
或null
,这将永远不会达到,所以条件的排序非常重要。
如果您没有声明为数组的变量,则可以创建一个检查:
if(x && x.constructor==Array && x.length){
console.log("is array and filed");
}else{
var x= [];
console.log('x = empty array');
}
这将检查变量x是否存在,如果存在,则检查它是否为填充数组。否则它会创建一个空数组(或者你可以做其他的东西);
如果您确定创建了一个数组变量,则可以进行简单的检查:
var x = [];
if(!x.length){
console.log('empty');
} else {
console.log('full');
}
你可以查看my fiddle here,显示最可能的方法来检查数组。
你应该做这个
if (!image_array) {
// image_array defined but not assigned automatically coerces to false
} else if (!(0 in image_array)) {
// empty array
// doSomething
}
如果不存在则不会导致异常的简单方法并转换为boolean:
!!array
例:
if (!!arr) {
// array exists
}
当您创建image_array时,它是空的,因此您的image_array.length为0
如下面的评论所述,我根据这个question's answer编辑我的答案):
var image_array = []
在else括号内部不会改变代码中之前定义的image_array
在ts
isArray(obj: any)
{
return Array.isArray(obj)
}
在HTML中
(照片== undefined ||!(isArray(photos)&& photos.length> 0))
一种现代的方式,ES5 +:
if (Array.isArray(array) && array.length) {
// array exists and is not empty
}
老派方式:
typeof array != "undefined"
&& array != null
&& array.length != null
&& array.length > 0
紧凑的方式:
if (typeof array != "undefined" && array != null && array.length != null && array.length > 0) {
// array exists and is not empty
}
CoffeeScript方式:
if array?.length > 0
案例未定义 未定义变量是一个尚未为其分配任何内容的变量。
let array = new Array(); // "array" !== "array"
typeof array == "undefined"; // => true
Case Null 一般来说,null是缺少值的状态。例如,当您错过或无法检索某些数据时,变量为null。
array = searchData(); // can't find anything
array == null; // => true
案例不是数组
Javascript有一个动态类型系统。这意味着我们不能保证变量保持什么类型的对象。我们有可能不会和Array
的实例交谈。
supposedToBeArray = new SomeObject();
typeof supposedToBeArray.length; // => "undefined"
array = new Array();
typeof array.length; // => "number"
案例空数组
现在,由于我们测试了所有其他可能性,我们正在谈论Array
的一个实例。为了确保它不是空的,我们询问它所持有的元素的数量,并确保它具有多于零的元素。
firstArray = [];
firstArray.length > 0; // => false
secondArray = [1,2,3];
secondArray.length > 0; // => true
怎么样(ECMA 5.1):
if(Array.isArray(image_array) && image_array.length){
// array exists and is not empty
}
这就是我使用的。第一个条件包括truthy,它具有null和undefined。第二个条件检查空数组。
if(arrayName && arrayName.length > 0){
//do something.
}
或者感谢tsemer的评论我添加了第二个版本
if(arrayName && arrayName.length)
然后我在Firefox中使用Scratchpad对第二个条件进行了测试:
var array1;
var array2 = [];
var array3 = ["one", "two", "three"];
var array4 = null;
的console.log(数组1);的console.log(数组2);的console.log(ARRAY3);的console.log(array4);
if(array1 && array1.length){
console.log("array1! has a value!");
}
if(array2 && array2.length){
console.log("array2! has a value!");
}
if(array3 && array3.length){
console.log("array3! has a value!");
}
if(array4 && array4.length){
console.log("array4! has a value!");
}
然后结果:
未定义
[]
[“一二三”]
空值
最后的测试显示
if(array2 && array2.length){
是相同的
if(array2 && array2.length > 0){
ARRAY3!有价值!
你应该使用:
if (image_array !== undefined && image_array.length > 0)
如果要测试是否已定义图像数组变量,可以这样做
if(typeof image_array === 'undefined') {
// it is not defined yet
} else if (image_array.length > 0) {
// you have a greater than zero length array
}
您可以使用jQuery的isEmptyObject()
来检查数组是否包含元素。
var testArray=[1,2,3,4,5];
var testArray1=[];
console.log(jQuery.isEmptyObject(testArray)); //false
console.log(jQuery.isEmptyObject(testArray1)); //true
JavaScript的
( typeof(myArray) !== 'undefined' && Array.isArray(myArray) && myArray.length > 0 )
负载和下划线
( _.isArray(myArray) && myArray.length > 0 )
这个怎么样 ?检查未定义数组的长度可能会引发异常。
if(image_array){
//array exists
if(image_array.length){
//array has length greater than zero
}
}