How to add data with unique keys via JSON/AJAX?
How to add data with unique keys via JSON/AJAX?
Hi guys!
I'd appreciate any insight here. I have a datatable which uses JSON data and pulls via AJAX.
The goal is to utilize data from arrays with unique key values. Unfortunately, I only get [object Object] as the response.
What would be the best way to pull such data?
Here is my code:
Javascript:

HTML:

| Name |
|---|
JSON:

{
"data":[
{
"CVS":[
{
"name":"lisinopril"
},
{
"name":"adderall"
}
]
},
{
"Costco":[
{
"name":"ibuprofen"
},
{
"name":"methylphenidate"
}
]
}
]
}
Ultimately, each primary category (CVS, Costco, etc) will have multiple keys, such as 'dosage', but I simplified the code to make it easier to understand what I'm looking to do.
Admittedly, I'm only just now learning the basics of Javascript, let alone libraries/frameworks like datatables, so I sincerely apologize for my lack of understanding. Again, any help/input is highly appreciated.
Thanks,
-Dave
Answers
This doesn't actually mean anything to DataTables for the
columns.dataoption.In fact it is that which explains why you are seeing
Object objectsince thatdatavalue is being converted to be a string.Moreover, DataTables will take data from a single array for the rows. Each entry in that array must be a row. It cannot read column information from multiple arrays as you appear to be doing above.
More information about this is available here.
Allan