Checking that a Predicate is True for All Typescript Array Elements

Ole Ersoy
Nov 5, 2021
Image by Jimmy Lau from Pixabay

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(allNumbers)}`);

Demo

--

--