marice juguilon
marice juguilon

Reputation: 1

how can I remove the quote or pass this string as normal array

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

Answers (3)

Sahil Gupta
Sahil Gupta

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);

enter image description here

Upvotes: 0

ajai Jothi
ajai Jothi

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);

output

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

cymruu
cymruu

Reputation: 3008

It's a JSON string. just use JSON.parse and you'll get array of objects.

Upvotes: 2

Related Questions