Checking that a Predicate is True for All Typescript Array Elements

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

Founder of Firefly Semantics Corporation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Mock Neo4j Calls in Node

Table to Excel using Sheet.js

Setup React-Native Font: The Easy Way

Algorithms Series Pt. 1

Firefly Semantics Validator API Basics

Migrate easily from the default Angular internationalization system to a Json-based translation…

What is JSX in React?

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
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation

More from Medium

How To Create A Data table in Angular 10 Using Bootstrap And Contrast

Formatting Javascript Strings and Numbers to 2 Decimal Places

javascript use strict

JavaScript Control Flow & Error Handling