计算数组中的对象?

时间:2016-03-02 18:16:20

标签: javascript arrays object count

我有一个这样的数组:

[{"this":"that","int":5},{"this":"that","int":5}]

如何用Javascript计算数组内的对象数({})?

提前致谢。

5 个答案:

答案 0 :(得分:5)

[{"this":"that","int":5},{"this":"that","int":5}].length; // 2

答案 1 :(得分:2)

尝试,

var cnt = 0;
var arr = [5 , 3 , "not an object" , {"this":"that","int":5},{"this":"that","int":5}];

arr.forEach(function(itm){
 if(!itm.__proto__.__proto__){
  cnt++;
 }
});

console.log(cnt + "normal objects are there"); //2

答案 2 :(得分:1)

var length = arrayName.length;

答案 3 :(得分:0)

就像这样:

$(function () {

gridContainer = $('#shuffleContainer');
var sizer = gridContainer.find('.col-md-4');

$('.js-shuffle-search').on('keyup', function () {
    var val = this.value.toLowerCase();
    gridContainer.shuffle('shuffle', function ($el, shuffle) {

        // Only search elements in the current group
        if (shuffle.group !== 'all' && $.inArray(shuffle.group, $el.data('groups')) === -1) {
            return false;
        }

        var text = $.trim($el.find('.picture-item__title').text()).toLowerCase();
        return text.indexOf(val) !== -1;
    });
});

当您拥有var yourArrayName = arrayName.length 并使用array时,它会计算.length中有多少objects

答案 4 :(得分:0)

数组[]中的

对象{}被视为项目,因此您可以找到计算项目的数组长度

var $data=[{"this":"that","int":5},{"this":"that","int":5}];

    var count=$data.length