lodash some

The lodash _.some collection method can be used to test to see if just one element of an array, or key of an object in general meets a given condition. There is another collection method known as _.every that works in a similar way to that of the _.some method but will only return true when all elements of a collection meet a given condition. In this post I will be going over some simple examples of both, as well as some ways of doing so with just plain old vanilla js as well.

1 - lodash some basic example

The lodash some method is used to quickly find out if at least one element in a collection meets a given condition. To use it just call the _.some method followed by the collection that is to be tested, followed by a function that will be used to test potentially all elements in the collection for a certain condition.

1
2
3
4
5
6
7
8
9
10
11
12
13
let _ = require('lodash');
// some arrays
let arr1 = ['foo', null, 42, 'bar'],
arr2 = ['foo', 'man', 'chew'],
// what to test for
tester = function (el) {
return typeof el === 'number';
};
console.log(_.some(arr1, tester)); // true
console.log(_.some(arr2, tester)); // false

2 - lodash some vanilla JavaScript alternatives

In this section I will be covering some quick vanilla ja examples of how to do what the _.some method in lodash can do with just plain old javaScript by itself. In some situations it is not to hard to just loop over the contents of an array or Object and just test to see if one object key value meets a given condition. So lets take look at some quick examples of doing what the _.some method can do with just plain vanilla javaScript.

2.1 - Just use a loop

So it is not ti hard to just use a while loop for example to loop over the contents of an array and check to see if one element in the array meets a certain condition of course.

1
2
3
4
5
6
7
8
9
10
11
12
let arr = ['foo', 42, null, 'baz'];
let i = arr.length,
some = false;
while (i--) {
if (arr[i] === null) {
some = true;
break;
}
}
console.log(some); // true

2.2 - Writing a some method

It is not to hard to write a some method of my own. The Object.keys method can be used to get all publc keys of an Object in general. This can then be used as a way to loop over all they keys and call a given test function that will return true if an element meets the desired condition. Such a method will work just fine with most Arrays and Object collections in general.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
let some = function (col, tester) {
let i = 0,
keys = Object.keys(col),
len = keys.length;
while (i < len) {
if (tester(col[keys[i]], keys[i], i)) {
return true;
}
i += 1;
}
return false;
};
// works with arrays
let arr = [null, 'foo', 'baz', 42, {}, false, true];
console.log(some(arr, function (el) {
return typeof el === 'number';
})); // true
console.log(some(arr, function (el) {
return typeof el > 50
})); // false
// works with objects in general
let obj = {
x: 42,
y: 17,
z: -12
};
console.log(some(obj, function (n) {
return n > 50 || n < -50
})); // false
console.log(some(obj, function (n) {
return n <0
})); // true