Returning true if JavaScript array contains an element

后端 未结 4 612
心在旅途
心在旅途 2021-01-28 01:12

So far I have this code:

var isMatch = viewedUserLikedUsersArray.indexOf(logged_in_user);
    if (isMatch >=0){
      console.log(\'is match\');
    }
    els         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-28 01:46

    You could just use Array.prototype.some

    var isMatch = viewedUserLikedUsersArray.some(function(user){
      return user === logged_in_user;
    });
    

    It stops when it finds a true value

提交回复
热议问题