Issue
I could not find exact answer for my problem, just want a way to determine something like
const numArray = [1,2,3,4];
const objArray = [{thing: value}, {thing: value}];
if (numArray typeof number[]) { console.log('This is an array of numbers.') }
if (objArray typeof object[]) { console.log('This is an array of objects.') }
Hope this explains even though I know is not correct as I am just learning.
Solution
You can check the type of the first item of the array and assume that all items have the same type:
const numArray = [1,2,3,4];
const objArray = [{thing: 'value'}, {thing: 'value'}];
if (typeof numArray[0] === 'number') {
console.log('This is an array of numbers.');
}
Or you can use Array.every() to check all of them:
if (objArray.every((item) => typeof item === 'object')) {
console.log('This is an array of objects.');
}
Answered By - axiac
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.