Page 1 of 1

Get the length of a JSON object in JS

Posted: 05 Dec 2020, 06:45
by Marius
I have converted a database table into a JSON object.
This is in essence a two-dimensional array, with each record being the first dimension, which is an enumerated array, and the second dimension of fields, an associative array.

Code: Select all

var act_obj_array ={
1: {act_id: "3", name: "Bruce Waelbrock Music Scholarship", category: "Life"},
2: {act_id: "4", name: "Christmas Childrens Gift Drive", category: "Community"},
3: {act_id: "5", name: "Church Drive", category: "Faith"},
4: {act_id: "6", name: "Church Kiosk", category: "Faith"},
5: {act_id: "7", name: "Consecration of the Holy Family", category: "Family"}
}
The number of the enumerated records, in the above example, 1-5, so that the number is this example would be '5'.

I've tried something like console.log(act_obj_array.length); , but the console displays undefined.

Get the length of a JSON object in JS

Posted: 05 Dec 2020, 08:19
by MarPlo
You can use Object.keys to get the list of keys in the Object as an array and then get the length from it:

Code: Select all

Object.keys(act_obj_array).length;