Check if Object Array and ignore all else

This question already has an answer here:

  • Check if object is array? 38 answers

  • You can use the isArray function from jQuery:

    if (jQuery.isArray(specs)) {
       $overview.html('<div class="bullet_spec"></div>');
       jQuery.each(specs, function () {
           $overview.children('div').append('<ul class="specs"><li class="label">' + this.k + ' : ' + this.v + '</li></ul>');
       });
    }
    

    However, it appears the problem in your fiddle is that some of the elements ( x ) aren't even Json. So it's not that the result isn't an array, it's that it is unable to parse the result at all. You can simply wrap your parsing script with a try/catch to handle this gracefully:

    var $overview = jQuery(this), spec;
    try {
        specs = jQuery.parseJSON($overview.html());
    } catch(e) {
        return;
    }
    

    Demonstartion


    这是用于检查数组无错误的常用代码和交叉浏览器代码:

    if (Object.prototype.toString.call( someVar ) === '[object Array]' ) {
    
    链接地址: http://www.djcxy.com/p/27574.html

    上一篇: 如何搜索JavaScript数组中的值?

    下一篇: 检查对象数组是否忽略所有其他