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)

0

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.