Checking that a Predicate is True for All Typescript Array Elements

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store