So far I have this code:
var isMatch = viewedUserLikedUsersArray.indexOf(logged_in_user);
if (isMatch >=0){
console.log('is match');
}
else {
console.log('no match');
}
If an element is in an array it will return a number greater or equal to 0 and so I can say isMatch >=0
but this doesn't seem the safest way. How else can I return a true/false from the desired code?
So far I have this code:
var isMatch = viewedUserLikedUsersArray.indexOf(logged_in_user);
if (isMatch >=0){
console.log('is match');
}
else {
console.log('no match');
}
If an element is in an array it will return a number greater or equal to 0 and so I can say isMatch >=0
but this doesn't seem the safest way. How else can I return a true/false from the desired code?
- 3 What's not safe about this? It's just how the function works. – laurent Commented Sep 28, 2016 at 14:13
-
That's the defined behavior of
.indexOf()
- a return value of-1
means the value is not in the array, and any other value means that it is. – Pointy Commented Sep 28, 2016 at 14:15 -
What do you mean by safest way? Where would this fail? You could use
.find
or.includes
. – Sebastian Simon Commented Sep 28, 2016 at 14:15 - Wrap it within a function, and return a boolean instead of logging to the console. – Teemu Commented Sep 28, 2016 at 14:15
- 2 there's an ES2015 alternative ... Array#includes – Jaromanda X Commented Sep 28, 2016 at 14:16
5 Answers
Reset to default 6You 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
Probably going to get blasted for some reason but hey, why not!
function findMatch(arr, user) {
var i = 0, count = arr.length, matchFound = false;
for(; i < count; i++) {
if (arr[i] === user) {
matchFound = true;
break;
}
}
return matchFound;
}
var isMatch = findMatch(viewedUserLikedUsersArray, logged_in_user); // etc.
An alternative could also be to use includes()
var isMatch = viewedUserLikedUsersArray.includes(logged_in_user); // returns true/false
The One and Only ChemistryBlob answer with Array.prototype
Array.prototype.findMatch = function(token) {
var i = 0, count = this.length, matchFound = false;
for(; i < count; i++) {
if (this[i] === token) {
matchFound = true;
break;
}
}
return matchFound;
}
var isMatch = viewedUserLikedUsersArray.findMatch(logged_in_user); // etc.
Use some as it allows the browser to stop as soon as one element is found that matches, so it's going to be faster:
const newArr = Array.some((e) => e.name === "amboji")
console.log(newArr); // returns true if it contains, else returns false
At first, we have to understand, what is stored in viewedUserLikedUsersArray
array.
If there are primitives, it's ok, but if there are objects we can't use indexOf
method of array, because it use strict parison ===
inside, how we know, the parison with object is going by link.
indexOf
works similar to iterating through the loop, the only way.
In case with objects we can use find
method of array MDN Array.prototype.find() or findIndex
Array.prototype.findIndex();
Also we can store users in hashMap with userId keys, and check matching by referring to the object property.
var someUsers = {
'#124152342': {
...
},
'#534524235': {
...
},
...
};
...
var someUserId = '#124152342';
if (someUsers[someUserId]) {
console.log('is match');
} else {
console.log('no match');
}