Count elements in a multidimensional array
-
26-10-2019 - |
Question
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
Solution
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])
.
OTHER TIPS
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);
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) {
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]
}
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