0

Hi I have a question about getting the names of properties of an object.

The context

I am retrieving some data from a database which I am trying to get the column headings from. I am carrying out an ajax request to get the data as JSON.

The code

var tableHeaders = "noHeaders";
var columns = [];
$( document ).ready( function( $ ) {
    $.ajax({
        "url": 'ajaxScripts/getDynamicData.php',
        "success": function(json) {

            tableHeaders = "";
            parsedData = JSON.parse(json);

            $.each(parsedData[0], function(i, val){
                console.log(parsedData[0]);
                tableHeaders += "<th>" + val + "</th>";
                columns.push(val);
            });

            $("#headings").append(tableHeaders);
            //$("#tableDiv").find("table thead tr").append(tableHeaders);

            $('#demotable').DataTable({
                dom: "Bfrtip",
                data: parsedData,
                columns: columns
            });
        }
    });
});

Table Sample

Name | Age | DOB | Height | Weight | Address | Hair Colour | Eye Colour


Steve| 24 | 22/11/1994 ...

The data gets returned as an array of Objects so I want to be able to retrieve the unique properties of those objects - The column headers (Name, Age, DOB etc)

Mr Dansk
  • 766
  • 3
  • 8
  • 23

1 Answers1

-1

The Object.keys() method seems to be what you are looking for if I understood correctly.

See How to list the properties of a JavaScript object as a possible answer.

Community
  • 1
  • 1
user3154108
  • 1,264
  • 13
  • 23