Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Instead of using a complex switch, it's better to simply get the information that is already present in your string.</p> <p>That is you should use</p> <pre><code>location = result.locationList[x].availability.split(':').pop().trim(); </code></pre> <p>The reasons it's better is that you won't have to maintain a list if the number of possible cases grows, and you won't risk to have a error in this list. By the way a shorter code is easier to manage (when it's readable).</p> <p>Notes :</p> <ul> <li><p>If you expect that your strings might not follow the model you give, you'll have to handle errors according to your application. For example you might want to issue an error message if <code>result.locationList[x].availability.split(':')</code> hasn't a length of 2.</p></li> <li><p>I assumed the <code>location</code> variable was already declared, that's why I didn't put <code>var</code>. If it isn't, add it : you can have many bugs coming from not explicitly declared variables.</p></li> </ul> <p>While error handling <em>has</em> to be dependent on the strings you expect and how you want to react, here's how you could organize them (here using a regex but that's the same) :</p> <pre><code>var m = result.locationList[x].availability.match(/[^\:]*:\s?(.*)/); if (m.length==2) { var location = m[1]; // use location } else { // the input string isn't good, maybe it's empty or // it doesn't contain the : char. Alert the user or the police // or use a default value instead } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload