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.');
}
CLICK HERE to find out more related problems solutions.