0

I have the following jquery script

success: function(data) {
                    //$("#divReport").html(CreateReportHTML(data));
                    $.each(data, function(key, value) {
                        createDynamicTable($("#tbReport"), key, 5);
                    });    

                    $("#btnViewReport").attr("disabled",false);
                    $("#btnExportToExcel").attr("disabled",false);
                },

from the above, "key" is meant for index as you know while "value" has the columns. The values are retirved like value.FirstName, value.LastName etc.

I need to get the number of columns available in "value" from $.each. How will I find it ? I tried value.length, which is not working.

Sreejesh Kumar
  • 2,449
  • 12
  • 51
  • 72

2 Answers2

0

You could use the following to retrieve the number of keys in the 'value' object.

Object.keys(value).length
Seidr
  • 4,946
  • 3
  • 27
  • 39
0

You can count the number of properties/keys of any object in Javascript using Object.keys() (reference) method. In your case that would be:

Object.keys(value).length

Reference:

  1. How to efficiently count the number of keys/properties of an object in JavaScript?
  2. Browser support
Community
  • 1
  • 1
Viral Patel
  • 8,506
  • 3
  • 32
  • 29