title | shortTitle | type | language | tags | cover | excerpt | dateModified | |
---|---|---|---|---|---|---|---|---|
Check if a JavaScript array has only one or many matches |
Array has only one or many matches |
story |
javascript |
|
interior-10 |
Learn how to check if an array has one or more values matching the given function, and how to find the matching elements. |
2024-01-30 |
Finding values in an array that match a given condition is one of the most common tasks when it comes to working with arrays. Luckily, JavaScript's Array
methods never cease to be of help, allowing to easily perform such operations.
Using Array.prototype.filter()
and Array.prototype.length
, you can easily check if an array has only one value matching the given function.
const hasOne = (arr, fn) => arr.filter(fn).length === 1;
hasOne([1, 2], x => x % 2); // true
hasOne([1, 3], x => x % 2); // false
To find the index of the only matching element, you can use Array.prototype.findIndex()
.
const findIndex = (arr, fn) => arr.findIndex(fn);
findIndex([1, 2, 3, 4], x => x % 2); // 0
findIndex([2, 4, 6, 8], x => x % 2); // -1
In order to check if an array has more than one value matching the given function, you can use the same approach. The only difference is that you need to change the comparison operator from ===
to >
.
const hasMany = (arr, fn) => arr.filter(fn).length > 1;
hasMany([1, 3], x => x % 2); // true
hasMany([1, 2], x => x % 2); // false
Finding the indexes of all matching elements is a little more involved. You need to use Array.prototype.reduce()
to loop over elements and store the indexes of matching elements.
const indexOfAll = (arr, val) =>
arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0, 3]
indexOfAll([1, 2, 3], 4); // []