Walking through an array in IF conditional in Javascript
I am creating a very lightweight search functionality that accepts the following:
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
searchStr == people.skills.forEach(function(x) { return x })) {
Results.push(people)
}
searchStr
- search line; "people" is a person object that has one first and last name and job, but only one of those that is an array - people.skills - is there anyway, I can go through this array in if-conditional with anonymous function?
source to share
You can use Array.prototype.some
to evaluate whether a condition is true for at least one entry in an array. It is better than Array.prototype.filter
that, because he will leave the first entry early to judge it as true.
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
people.skills.some(function(x) { return searchStr == x })) {
Results.push(people)
}
Array.prototype.some
is new in Ecmascript 5 and is fairly well supported now .
You can improve your search by doing a partial search for terms instead of an exact search, toggling comparison comparisons with Array.prototype.indexOf
to see if the string contains the search term.
if (contains(people.first_name, searchStr) ||
contains(people.last_name, searchStr) ||
contains(people.job, searchStr) ||
people.skills.some(function(x) { return contains(x, searchStr); })) {
Results.push(people)
}
function contains(a, b) {
return a.indexOf(b) >= 0;
}
source to share
For this purpose is used Array.prototype.some
:
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
people.skills.some(function(skill) {
return searchStr == skill;
})) {
Results.push(people)
}
But the use of an anonymous function is not necessary in this case. We can use the method Array.prototype.indexOf
:
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
people.skills.indexOf(searchStr) != -1) {
Results.push(people)
}
or, more idiomatic:
people.skills.indexOf(searchStr)+1
Keep in mind that Array.prototype.every
, some
, forEach
, filter
, reduce
and reduceRight
are part of the ECMAScript 5 and are therefore not supported in older browsers that only support ECMAScript <= 3.1. You should use a polyfill if that's the problem.
source to share
You can use indexOf to determine if a value exists in an array using javascript if you really need to step through the array and then use some function like @DanielImms recommend.
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
people.skills.indexOf(searchStr)>-1) {
Results.push(people)
}
source to share
var searchSkills = [].concat.apply([], people.skills);
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
searchSkills.indexOf(searchStr) >= 0 {
Results.push(people)
}
I'll leave this for posterity, but this method will probably be useful if people.skills
multidimensional.
As a side note, I would suggest that it is indexOf
much more efficient than doing filter
map
or some
.
source to share
And for the sake of thoroughness, if you were at the forefront and with Ecmascript 7, you could use Array.prototype.includes()
if (searchStr == people.first_name ||
searchStr == people.last_name ||
searchStr == people.job ||
people.skills.includes(searchStr)
)
source to share