One more effective method to verify the type of an Array containing elements of any type is to use the Array.isArray()
method along with the Array.every()
method. This method checks if the array is an array and if every element of the array satisfies a certain condition. For example, to verify if every element in the array is of type 'string':
let array = [1, 'hello', true];
if (Array.isArray(array) && array.every((element) => typeof element === 'string')) {
console.log('Array is of type string');
} else {
console.log('Array is not of type string');
}
This method checks if the array is an array and if every element in the array is of type 'string'. If every element satisfies the condition, it will return true, otherwise it will return false.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-10-05 11:00:00 +0000
Seen: 15 times
Last updated: Jul 29 '21
What is the method for obtaining an element from a Firestore array with a unique value?
How can user input be added to an array using a loop in C# and then displayed through printing?
How can the objects that are duplicated within a nested array be removed?
How can I resolve the ValueError regarding incompatible shapes between (None, 20, 9) and (None, 9)?
What is the way to add up items in an array with two dimensions using Google Apps Script?
How can an array be filtered using the values in another array?