Zantinger Zantinger - 17 days ago 9
Javascript Question

validate the correct types of a collection with lodash

I'm a little bit confusing about how to check a collection(object) with lodash _.some() function.

var x = {prop1: 800, prop2: 800, prop3: 'test'};

_.some(x, i => _.isNaN(i))
//false

_.some(x, _.isNaN)
//false

Answer

Now that you want to know if all of the properties are numbers, you can use this:

!_.some(x, i=> !_.isNumber(i))

I don't know about the perforamance differences of _.some and _.every but you could of course also use:

_.every(x, i=> _.isNumber(i))