add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.every

Overview

Lodash is a popular JavaScript library with a wide range of useful utility functions. One such function is the _.every method. The purpose of this method is to determine whether all elements in a collection pass a specific condition or not.

The syntax for using _.every is straightforward. It takes two parameters: the collection (an array or object) and a function that evaluates each element in the collection. The function takes one argument, which is the current element being evaluated.

Here is an example of using _.every on an array of numbers:

const numbers = [1, 2, 3, 4, 5];
const areAllPositive = _.every(numbers, (num) => num > 0);
console.log(areAllPositive); // true

In this example, _.every checks if all elements in the numbers array are greater than 0. Since this condition is true for all elements, areAllPositive is set to true.

If any element fails the condition, _.every returns false. For example:

const numbers = [1, 2, 3, -4, 5];
const areAllPositive = _.every(numbers, (num) => num > 0);
console.log(areAllPositive); // false

In this case, the fifth element (-4) fails the condition, so areAllPositive is set to false.

_.every can also be used with objects. Here is an example:

const person = {
  name: "Alice",
  age: 30,
  occupation: "Engineer"
};

const isStringType = (value) => typeof value === "string";

const areAllStringProperties = _.every(person, isStringType);
console.log(areAllStringProperties); // false

In this example, _.every is used to check if all properties of the person object are strings. Since the age property is a number, areAllStringProperties is set to false.

_.every is a useful method when you need to check if an entire collection meets a certain condition. Keep in mind that _.every stops iterating as soon as an element fails the condition so it can be very performant.