How to read data from an Array contained in an object (Nested object data (objects))
How to read data from an Array contained in an object (Nested object data (objects))
I have implemented a table fed with JSON data.
The data structure is nested as in the example described on the page:
https://datatables.net/examples/ajax/deep.html
Write a procedure that retrieves data from the onclick event in a table row
data = table.row(this).data()
I retrieve data from objects without any problems. For example:
let start_date = data["hr"]["start_date"]
I have a problem getting data from Arrays objects. I tried different syntaxes, e.g.:
let contact_city = data["contact"][0]
where 0 means the first object in the array.
The console returns an error: Uncaught TypeError: Cannot read properties of undefined (reading '0')
I am sending some of the data returned from data = table.row(this).data()
Please help
This question has an accepted answers - jump to answer
Answers
I built a test case based on the example you linked to. Using
let contact_city = data["contact"][0]
should work. The test case shows four ways to access the nested array:https://live.datatables.net/haqelade/1/edit
If this doesn't help please provide a link to your page or a test case replicating the issue you are having.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Good morning,
thank you for your reply and indicating the solution https://live.datatables.net/
I tested referencing the data source (JSON). Retrieving the values of individual fields works correctly.
I removed the old code and wrote it from scratch. Now it works properly.
I made a reference to data - data["contact"][0] - from another function. I suspect I was passing a string instead of a numeric value. I'll check it out next time I get a chance.
Thank you for your help. I will use https://live.datatables.net/ more often.