I have a problem with JavaScript, I have an array that get from model in controller and send it from controller to view like : echo json_encode($data); when i printed console.log(data) in view I got data like :
[{"id":"1","u_name":"07991111111"}]
Now I want to get the specific data like user_name. how can i get ?
I tried
console.log(data.user_name)
Try this:
That is data[0]
alert(data[0].id) // "1"
alert(data[0].u_name) // "07991111111"
try this way
var data=[{"id":"1","u_name":"07991111111"}];
var id=data[0].id;
var name=data[0].u_name;
console.log('id',id);
console.log('name',name);
if data is more than 0
like
var data=[{"id":"1","u_name":"07991111111"},
{"id":"2","u_name":"07991111112"}
];
than you will access that array object like this
var id1=data[0].id;
var id2=data[1].id;
Try this:
var obj = JSON.parse('{"id":"1","u_name":"07991111111"}');
console.log(obj.id);
console.log(obj.u_name);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.