当页面第一次加载时,我需要检查image_array中是否有图像,并加载最后一张图像。

否则,我禁用预览按钮,提醒用户按新的图像按钮,并创建一个空数组来放置图像;

问题是else中的image_array一直被触发。如果一个数组存在-它只是覆盖它,但警报不工作。

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; ?>

当前回答

检查数组是否为空

一个现代的方式,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

Why?

情况下定义 未定义变量是一个你还没有给它赋值的变量。

let array = new Array();     // "array" !== "array"
typeof array == "undefined"; // => true

情况下空 一般来说,null是缺少值的状态。例如,当您错过或未能检索某些数据时,变量为空。

array = searchData();  // can't find anything
array == null;         // => true

Case不是数组 Javascript有一个动态类型系统。这意味着我们不能保证变量持有什么类型的对象。有可能我们不是在与Array的实例对话。

supposedToBeArray =  new SomeObject();
typeof supposedToBeArray.length;       // => "undefined"

array = new Array();
typeof array.length;                   // => "number"

Case空数组 现在,由于我们测试了所有其他可能性,我们正在与Array的一个实例对话。为了确保它不是空的,我们询问它所持有的元素的数量,并确保它的元素大于0。

firstArray = [];
firstArray.length > 0;  // => false

secondArray = [1,2,3];
secondArray.length > 0; // => true

其他回答

检查数组是否为空

一个现代的方式,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

Why?

情况下定义 未定义变量是一个你还没有给它赋值的变量。

let array = new Array();     // "array" !== "array"
typeof array == "undefined"; // => true

情况下空 一般来说,null是缺少值的状态。例如,当您错过或未能检索某些数据时,变量为空。

array = searchData();  // can't find anything
array == null;         // => true

Case不是数组 Javascript有一个动态类型系统。这意味着我们不能保证变量持有什么类型的对象。有可能我们不是在与Array的实例对话。

supposedToBeArray =  new SomeObject();
typeof supposedToBeArray.length;       // => "undefined"

array = new Array();
typeof array.length;                   // => "number"

Case空数组 现在,由于我们测试了所有其他可能性,我们正在与Array的一个实例对话。为了确保它不是空的,我们询问它所持有的元素的数量,并确保它的元素大于0。

firstArray = [];
firstArray.length > 0;  // => false

secondArray = [1,2,3];
secondArray.length > 0; // => true

使用uncore或lodash:

_isarray (image_array) && ! ipad (image_array)

你可以使用jQuery的isEmptyObject()来检查数组是否包含元素。

var testArray=[1,2,3,4,5]; 
var testArray1=[];
console.log(jQuery.isEmptyObject(testArray)); //false
console.log(jQuery.isEmptyObject(testArray1)); //true 

来源:https://api.jquery.com/jQuery.isEmptyObject/

一个简单的方法,不会导致异常,如果不存在,并转换为布尔值:

!!array

例子:

if (!!arr) {
  // array exists
}

在ts

 isArray(obj: any) 
{
    return Array.isArray(obj)
  }

在html中

(isArray(photos) && photos. isArray == undefined || !长度> 0))