Search code examples
javascriptlodash

validate the correct types of a collection with lodash


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

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

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

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

Solution

  • 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))