Reputation: 301
Good Day, is it possible to convert an object to an Actioscript model in Javascript ? I have this:
const user = [{"id":"1","name":"Doe","firstname":"John","tel":"1112223333"}];
I would like to have this:
const user = [{id:1,name:"Doe",firstname:"John",tel:1112223333}];
When I use user.replace(/"/g,"");
I have the error:
user.replace is not a function
But this is where I'm stuck. I don't know how to do it if I can't use replace
.
To put you in context, the object is fetched via ajax and PHP by doing echo json_encode($to_encode);
Thank You for your help! :)
Upvotes: 0
Views: 39
Reputation: 22500
Its a JSON.parse()
Updated
convert the string to number
const user ='[{"id":"1","name":"Doe","firstname":"John","tel":"1112223333"}]';
var res =JSON.parse(user)
res.forEach(function(a){ //convert the string to number
Object.keys(a).forEach(function(key){
a[key] = Number(a[key])||a[key]
})
})
console.log(res)
Check you broswer console.log F12
is showen like this
Upvotes: 1