You can use array#some
at this context,
var data = [
'one',
'two',
'three',
'four',
'three',
'five',
];
found = data.some(function(x) {
return x == "three";
});
console.log(found); // true or false
If you use filter
, then the array will be filtered based on the truthy value returned inside of the callBack
function. So if any matches found meaning, if the function returned with value true
, then the element on that particular iteration will be collected in an array
and finally the array will be returned.
Hence in your case ["three", "theree"]
will be returned as a result. If you dont have any "three"
, then an empty array would be returned, At this context you have to make a additional check to find the truthy value as a result.
For Example:
var res = arr.filter(itm => someCondition);
var res = !!res.length;
console.log(res); //true or false.
So to avoid that over killing situation we are using Array#some.