1

I'm using a large API that contains a JSON file of TV Show information.

The key 'name' contains guest information. Most of them return with the list of guests, however some return as 'Episode [x]'. For example:

for (x in data){
 let title = data[x]._embedded.show.name;
 let guests = data[x].name;
 let airdate = data[x].airdate;

 switch(title){
   // ...
   case 'The Daily Show with Trevor Noah':
      p.innerHTML = airdate + " " + guests;
      noah.appendChild(p);
      console.log(airdate, guests);
      break;
   default:
      break;
 }
}

Guest values return as:

Kevin Young, Antoinette Robertson, Gen. Michael Hayden, David Blaine, Episode 63, Episode 64, Episode 65

I'd like to display just the names, and somehow remove any instance of 'Episode'. I have a few ideas, but I'm new to JavaScript and having some trouble. If more code is necessary to answer this question, I'll update this question. Thanks in advance

5
  • 1
    If code is necessary to answer this question, I'll update this with necessary information. Yes, always post the code you've tried with your question if you want help debugging it. Commented May 1, 2018 at 0:29
  • Sure thing, I just updated my question Commented May 1, 2018 at 0:33
  • I don't think that a switch(title){ is returned by a JSON call... Rather a valid JSON. Commented May 1, 2018 at 0:34
  • take a look at replace function Commented May 1, 2018 at 0:43
  • @AmrAly Replacing does remove 'Episode' but I want to remove the entire value that contains 'Episode'. Otherwise, the episode numbers still exist. Wondering if there's some sort of 'contains' method that will remove all instances Commented May 1, 2018 at 1:05

3 Answers 3

1

If you're looking to only print out items that don't have episode in the title, you could put an if() statement before your switch(), and then use a continue statement if you come across an invalid item to skip it (continue will tell Javascript to basically skip to the next item).

for (x in data){
  let title = data[x]._embedded.show.name;
  let guests = data[x].name;
  let airdate = data[x].airdate;

  if (guests.substr(0, 7) === 'Episode') continue;
  // if the title starts with "Episode", no code after this line will be run for this item

  switch(title){
    // ...
    case 'The Daily Show with Trevor Noah':
      p.innerHTML = airdate + " " + guests;
      noah.appendChild(p);
      console.log(airdate, guests);
    break;
    default:
    break;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked perfectly. If statement should start with 'guests' rather than 'title', but this does work.
1

How about using regex?

var pattern = /(,\s)?Episode\s\d+((,\s)?)/g;
var guests = data[x].name.replace(pattern, "");

Test cases:

var str1 = "Kevin Young, Antoinette Robertson, Gen. Michael Hayden, David Blaine, Episode 63, Episode 64, Episode 65"; 
var str2 = "Episode 63, Episode 64, Episode 65, Kevin Young, Antoinette Robertson, Gen. Michael Hayden, David Blaine"; 
var str3 = "Kevin Young, Antoinette Robertson, Gen. Michael Hayden, David Blaine";
var str4 = "Episode 63, Episode 64, Antoinette Robertson, Episode 65";
var str5 = "Episode 63, Episode 64, Episode 65";
var str6 = "Episode 63";

Comments

0

You can replace all the instances of Episode [0-9] like so:

var str = "Kevin Young, Antoinette Robertson, Gen. Michael Hayden, David Blaine, Episode 63, Episode 64, Episode 65";

var newStr = str.replace(/,[ ]?Episode[ ]?[0-9]+/g, '');

console.log(newStr);

// Kevin Young, Antoinette Robertson, Gen. Michael Hayden, David Blaine

Comments

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.