To start off, it is crucial to comprehend the contents of the viewedUserLikedUsersArray
array.
If the array contains primitives, then there's no issue. However, if it includes objects, using the indexOf
method of an array becomes problematic because it utilizes strict comparison with ===
, which means comparing objects by reference.
indexOf
essentially functions like iterating through a loop as the only option.
In scenarios involving objects, the application of the find
method in the array or MDN Array.prototype.find() and findIndex
Array.prototype.findIndex() can be considered;
Alternatively, storing users in a hashMap with userId keys and checking for matches by referencing the object property is another viable approach.
var someUsers = {
'#124152342': {
...
},
'#534524235': {
...
},
...
};
...
var someUserId = '#124152342';
if (someUsers[someUserId]) {
console.log('is match');
} else {
console.log('no match');
}