user2990425 user2990425 - 1 year ago 22
HTML Question

How to get the length of elements within this nested object

I want to create/read an object of a table (column by column). My code goes here:

var arr1 = [];
var arr2 = [];
for (var i = 0; i < 3; i++) {
var werteArray = [];
var ueberschriftArray = [];
$('#tabelleDateneingabe tbody tr').each(function() {
werteArray.push($(this).children().eq(i).text());
});
$('#tabelleDateneingabe thead tr').each(function() {
ueberschriftArray.push($(this).children().eq(i).text());
});
arr2.push({spalte: i, daten: {ueberschrift: ueberschriftArray, werte:werteArray}});
}
arr1.push(arr2);


This runs fine and produces this JSON:

[
[
{
"spalte": 0,
"daten": {
"ueberschrift": [
"Spalte1"
],
"werte": [
"Zelle: 0_0",
"Zelle: 1_0",
"Zelle: 2_0",
"Zelle: 3_0",
]
}
},
{
"spalte": 1,
"daten": {
"ueberschrift": [
"Spalte2"
],
"werte": [
"Zelle: 0_1",
"Zelle: 1_1",
"Zelle: 2_1",
"Zelle: 3_1",
]
}
}]]


Now I want to go through this object in order to get the number of elements for every row.

I'm pretty sure this could be done by using length on the "werte" node, but how to access this value for every column ("spalte")?

Any help is appreciated.

Answer Source

This will return an array of numbers of entries in werte;

var rowCount = [];
for(var i=0;i<json[0].length;i++) {
    var rowArr = json[0][i].daten.werte;
    rowCount[i] = rowArr.length;
}
console.log(rowCount);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download