Checking that a Predicate is True for All Typescript Array Elements | Task

Ole Ersoy
Nov - 01  -  1 min

Scenario

We have an array of elements [1,2,'a'], and we want to see whether all of the elements in the array are numbers.

Approach

This approach uses @fireflysemantics/validatorts to check whether the array element is a number:

let arr: any[] = [1, 2, 'a'];
function allNumbersPredicate(v: any, index: number, array: any[]):boolean {
   console.log(`is number ${isNumber(v).value}`);
   return isNumber(v).value;
}
console.log(`All numbers:? ${arr.every(allNumbersPredicate)}`);

Demo