计算多维数组中的元素 [英] Count elements in a multidimensional array

查看:72
本文介绍了计算多维数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

loadData : function(jsonArray) {
var id = $(this).attr("id");

for(var i in jsonArray) {
    $("#"+id+" tbody").append('<tr class="entry-details page-1 entry-visible" id="entry-'+i+'"></tr>');

    var header = {
        1: "time",
        2: "project",
        3: "task"
    }
    var col = 1;
    while(col <= jsonArray[i].length) {
        $("#"+id+" tbody #entry-"+i).append("<td>"+jsonArray[i][header[col]]+"</td>")
        col++
}}

将采用类似于以下内容的JSON数组

It will take a JSON array that looks similar to the following

{"1":{"project":"RobinsonMurphy","task":"Changing blog templates","time":"18\/07\/11 04:32PM"},"2":{"project":"Charli...

代码应遍历各行(这样做),然后遍历各行数据。

The code should loop through the rows (which it does), and then loop through the colums of data.

我面临的问题是为了将列数据放置在正确的列中,我需要计算一行中返回了多少数据我尝试了jsonArray [i] .length,但是返回的是不确定的。

The problem I am facing is in order to place the column data in the correct column, I need to calculate how many pieces of data are being returned in a row. I tried jsonArray[i].length, however this returns undefined.

任何帮助将不胜感激

推荐答案

您根本没有任何数组,只有对象。

You do not have any arrays at all, only objects.

要计算对象中的项目,请创建一个简单的函数ion:

To count items in an object, create a simple function:

function countInObject(obj) {
    var count = 0;
    // iterate over properties, increment if a non-prototype property
    for(var key in obj) if(obj.hasOwnProperty(key)) count++;
    return count;
}

现在,您可以调用 countInObject(jsonArray [i ])

这篇关于计算多维数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆