javascript - Check if value exists in an object in array of objects -


i trying write code of following algorithm

  • i have array of active_id (array)
  • id coming url (string)
  • if value of id not exist in active_id array
    • run function a()
  • else nothing.

note - function a() should run once.

i tried writing code

for (var = 0; < activeids.length; i++) {     if (activeids[i] != uid) {          a();  //this running multiple times.     } 

i tried using while loop

var = 0; while (activeids[i] != uid) {      a();  //this running multiple times again.     i++; } 

there missing. not able figure out.

you can use indexof function return -1 if element not exist on array , positive number start 0 till array (length -1) if element exist:

if (activeids.indexof(uid) == -1) {     a();   }  function a();  

Comments