0

With some help from fellow stack users I currently have this:

http://jsfiddle.net/XRCvE/

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.

3 Answers 3

3

JSFiddle

Just do a split on the , and take the first item:

events[0].location.city = events[0].location.city.split(",")[0];
Sign up to request clarification or add additional context in comments.

Comments

2

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/

Comments

2
if(events[0].location.city.indexOf(',') > 0) {
    events[0].location.city = events[0].location.city.split(",")[0];
}

http://jsfiddle.net/XRCvE/4/

1 Comment

+1 for split() instead of indexOf(); to me, using the former is a lot cleaner, and much more obvious as to what the code does than using indexOf().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.