EIGHTYFO
EIGHTYFO

Reputation: 21

JS - Check if an array position contains a word and then return another word

Ive got this problem where I need to check an arrayposition if the string contains this at the start:

as-se-gbg-XXXXXXXXXX-XXX-XX

I just need to check if the string contains the as-se-gbg stuff and then return it as Sweden, Gothenburg.

I tried to use this method:

if(b == "as-se-gbg") {
return("Göteborg");

But the == operator is trying to match the hole string...

And the other stuff thats marked xxxxxx isnt anything important at all...

Id be glad if there is anyone that could help me with this problem :D

Best Regards,

EIGHTYFO

Upvotes: 0

Views: 170

Answers (2)

gblazex
gblazex

Reputation: 50109

if (b.indexOf("as-se-gbg") === 0)

will be true if the pattern is at the beginning of the string. For more information see: indexOf

Upvotes: 3

bluesmoon
bluesmoon

Reputation: 4320

You need to use a regex match:

if(b.match(/^as-se-gbg-/) {
    return "Göteborg";
}

Upvotes: 0

Related Questions