How to access object inside array
How to access object inside array

hi all
i have tried using column render, and it return when theres single object on it
i've got data like this
"data":[
{
"id":"7",
"item_type":"",
"location_id":"1,3",
"product_id":"101",
"menu":"Mala Traditional Soup Handmade",
"category":{
"id":"1",
"category_name":"Soup",
"category_slug":"soup",
"softDelete":0,
"created_at":"2020-04-27 13:34:14",
"updated_at":"2020-04-03 21:54:49"
},
"location":[
{
"id":"1",
"location_name":"Chongqing Senopati",
"totalpax":"20"
}
},
{
"id":"3",
"location_name":"Chongqing Serpong",
"totalpax":"50"
}
}
]
},
i want to access data in "location_name"
already tried with this format, and just got "undefined"
columns: [
{
data: null,
render: function(data,type,row){
return 'data '+data['location']['location_name']+''},
orderable: false
},
]
thanks!
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Take a look at the documentation for the
columns.render
function. You should be usingrow
instead ofdata
to access the row data. if this doesn't help then please provide a test case showing the issue.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
hi
thanks for response
i could get the data with following format
but it just show first data only,
"location_name":"Chongqing Senopati",
how can i get all data?
hi
just for update, i can get this in console log
but it trow alert error
what data should i put there?
thanks!
As Kevin said, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
hi, right, sorry
on my way to create test case
thanks
Hi
sorry cant get working on codepen or etc
here the test page : http://hotpot.didev.id/items
the json http://hotpot.didev.id/items/json
many thanks!
According to the jQuery each() docs it seays this about using
return
:Once the loop is done the render function isn't returning anything for the column, resulting in the error you are seeing.
I took an example of you JSON response and put it in this example:
http://live.datatables.net/yetoqice/1/edit
It builds an array of
location_name
from thelocation
array. The example shows a couple of options of how toreturn
the array depending on how you want to display the data.Kevin
many thanks! @kthorngren
the correct path i tried before is just "data:location"
i new in js, never use function like "push" before.
now i learn some!
thanks!