Reputation: 1
events: "[{id:"3",title:"activity",start:"2017-03-11 00:00:00",allDay:true},{id:"4",title:"nutrition",start:"2017-03-11 00:00:00",allDay:true}]"
how can I remove the quote or pass this string as normal array in angularjs2?
Upvotes: 0
Views: 55
Reputation: 229
let input = {
events: '[{id:"3",title:"activity",start:"2017-03-11 00:00:00",allDay:true},{id:"4",title:"nutrition",start:"2017-03-11 00:00:00",allDay:true}]'
};
eval(obj.events);
Upvotes: 0
Reputation: 2294
Here, the string in events object is not a valid JSON. So u can't use JSON.Parse
here. For valid JSON key
should be wrapped in double quote eg: {id:...}
should be {"id":...}
let input = {
events: '[{id:"3",title:"activity",start:"2017-03-11 00:00:00",allDay:true},{id:"4",title:"nutrition",start:"2017-03-11 00:00:00",allDay:true}]'
};
let dataToSend = Object.assign({}, input);
dataToSend.events = eval(dataToSend.events);
console.log(dataToSend);
Since, your input sting is still a valid javascript object you can use eval
method for conversion. Here I used object.assign
to create a copy of your input. So, any modification to dataToSend
will not affect your actual input object.
Upvotes: 0
Reputation: 3008
It's a JSON string. just use JSON.parse and you'll get array of objects.
Upvotes: 2