Reputation: 4316
With some help from fellow stack users I currently have this:
I would like to know what I need to do to edit the code to change the JSON data {location.city} to delete after the first comma so this:
Mansfield, MA, US
becomes
Mansfield
Apologies, I am a beginner, a working jsfiddle would be much appreciated. Thank you.
Upvotes: 0
Views: 121
Reputation: 800
Just do a split on the ,
and take the first item:
events[0].location.city = events[0].location.city.split(",")[0];
Upvotes: 3
Reputation: 1893
if(events[0].location.city.indexOf(',') > 0) {
events[0].location.city = events[0].location.city.split(",")[0];
}
Upvotes: 2
Reputation: 14411
Here's one way to do it:
if (events[0].location.city.indexOf(',') > 0) {
events[0].location.city =
events[0].location.city.substr(0, events[0].location.city.indexOf(','));
}
I've updated the JSFiddle with a working example: http://jsfiddle.net/XRCvE/1/
Upvotes: 2