Searching for Unique Javascript Array Values with Some


We have an array containing the values [1,2,3,4,5] . We want to find the unique value 3 and then stop looking.


We’ll be using some to iterate over the array. The some function stops iterating after the first match that is true , which in our case is 3 .

let arr: number[] = [1, 2, 3, 4, 5];var unique: number = NaN;function findThree(value: number) {
unique = value;
return value == 3;
arr.some((v) => findThree(v));

We console log the value to see that we are only executing the loop 3 times.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation