计算多维数组中的元素数量

发布于 2024-11-26 09:34:59 字数 818 浏览 2 评论 0原文

我得到了这段代码:

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 数组

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

代码应该循环遍历行(确实如此),然后循环遍历数据列。

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

任何帮助将不胜感激

Ive got this code:

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++
}}

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.

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.

Any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑,眼淚并存 2024-12-03 09:34:59

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

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

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])

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

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

Now, you can call countInObject(jsonArray[i]).

赤濁 2024-12-03 09:34:59

像这样:

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the size of an object
var size = Object.size(myArray);

JavaScript 对象的长度

Like this:

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the size of an object
var size = Object.size(myArray);

Length of a JavaScript object

情何以堪。 2024-12-03 09:34:59

看看那个小提琴:
http://jsfiddle.net/Christophe/QFHC8/

for (var j in jsonArray[i]) {

代替

while (col <= jsonArray[i].length) {

Have a look to that fiddle :
http://jsfiddle.net/Christophe/QFHC8/

key is

for (var j in jsonArray[i]) {

instead of

while (col <= jsonArray[i].length) {
千仐 2024-12-03 09:34:59

jsonArray[i].length 不起作用,因为 jsonArray[i] 是字典而不是数组。你应该尝试这样的事情:

for(var key in jsonArray[i]) {
     jsonArray[i][key]
}

jsonArray[i].length will not work because jsonArray[i] is a dictionary not an array. You should try something like:

for(var key in jsonArray[i]) {
     jsonArray[i][key]
}
○闲身 2024-12-03 09:34:59

我一直面临同样的问题,我编写了一个函数来获取组合的多维数组/对象中的所有标量值。如果对象或数组为空,那么我认为它不是一个值,因此我不会对它们求和。

function countElements(obj) {
  function sumSubelements(subObj, counter = 0) {
    if (typeof subObj !== 'object') return 1; // in case we just sent a value
    const ernation = Object.values(subObj);
    for (const ipated of ernation) {
      if (typeof ipated !== 'object') {
        counter++;
        continue;
      }
      counter += sumSubelements(ipated);
    }
    return counter;
  }
  return sumSubelements(obj);
}

let meBe = { a: [1, 2, 3, [[[{}]]]], b: [4, 5, { c: 6, d: 7, e: [8, 9] }, 10, 11], f: [12, 13], g: [] };
const itution = countElements(meBe);
console.log(itution); // 13

let tuce = [1,2];
console.log(countElements(tuce)); // 2

console.log(countElements(42)); // 1

或者,如果您想在生产中使用它,您甚至可以考虑将其添加到对象原型中,如下所示:

Object.defineProperty(Object.prototype, 'countElements', {
  value: function () {
    function sumSubelements(subObj, counter = 0) {
      const subArray = Object.values(subObj);
      for (const val of subArray) {
        if (typeof val !== 'object') {
          counter++;
          continue;
        }
        counter += sumSubelements(val);
      }
      return counter;
    }
    return sumSubelements(this);
  },
  writable: true,
});

console.log(meBe.countElements()); // 13
console.log([meBe].countElements()); // 13; it also works with Arrays, as they are objects

I've been facing the same issue and I scripted a function that gets all the scalar values in a multidimensional Array/Object combined. If the object or array are empty, then I assume that it's not a value so I don't sum them.

function countElements(obj) {
  function sumSubelements(subObj, counter = 0) {
    if (typeof subObj !== 'object') return 1; // in case we just sent a value
    const ernation = Object.values(subObj);
    for (const ipated of ernation) {
      if (typeof ipated !== 'object') {
        counter++;
        continue;
      }
      counter += sumSubelements(ipated);
    }
    return counter;
  }
  return sumSubelements(obj);
}

let meBe = { a: [1, 2, 3, [[[{}]]]], b: [4, 5, { c: 6, d: 7, e: [8, 9] }, 10, 11], f: [12, 13], g: [] };
const itution = countElements(meBe);
console.log(itution); // 13

let tuce = [1,2];
console.log(countElements(tuce)); // 2

console.log(countElements(42)); // 1

Or if you want to use this in production, you could even think about adding it to the Object prototype, like this:

Object.defineProperty(Object.prototype, 'countElements', {
  value: function () {
    function sumSubelements(subObj, counter = 0) {
      const subArray = Object.values(subObj);
      for (const val of subArray) {
        if (typeof val !== 'object') {
          counter++;
          continue;
        }
        counter += sumSubelements(val);
      }
      return counter;
    }
    return sumSubelements(this);
  },
  writable: true,
});

console.log(meBe.countElements()); // 13
console.log([meBe].countElements()); // 13; it also works with Arrays, as they are objects
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文