0

I have an array of objects like this:

var locations = [
[{id: 1, lat: 51.52376322544537, lng: 5.13785702262885, content: 'Title A'}],
[{id: 2, lat: 51.52358632767757, lng: 5.137921395645208, content: 'Title B'}],
[{id: 3, lat: 51.52343946863126, lng: 5.138093057022161, content: 'Title C'}],
];

I want to access the id's now i do it like this:

if (id === 7 || id === 13 || id === 15 || id === 16) {
        marker.setIcon(icon4);
    }

I thought this was a shorter way:

if (id.toString().indexOf(['7','13','15','16'] > -1)) {
        }

But this is not working can somebody help me out on this, it is frustrating me because i don't like that code i use.

Sireini
  • 4,142
  • 12
  • 52
  • 91

2 Answers2

2

You can change it to

if ([7,13,15,16].indexOf(id) > -1 ) {
        marker.setIcon(icon4);
    }
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
1

you can use switch instead of if

switch(id)
{
case 7:
case 13:
case 15:
case 16:
marker.setIcon(icon4);
break;
}

in future if any new id is added it is easy to add only one case. I hope it will useful.

Ashish Pandya
  • 167
  • 10