Праверка на масіў у JavaScript
Існуе спецыяльны метад Array.isArray,
з дапамогай якога можна праверыць зменную
на тое, ці змяшчае яна сапраўдны масіў
ці не. Давайце праверым працу метаду.
Масіў:
let test = [1, 2, 3];
let res = Array.isArray(test);
console.log(res); // выведзе true
Аб'ект:
let test = {a: 1, b: 2, c: 3};
let res = Array.isArray(test);
console.log(res); // выведзе false
Прымітыў:
let test = 'abcde';
let res = Array.isArray(test);
console.log(res); // выведзе false
Псеўдамасіў:
let test = document.querySelectorAll('p');
let res = Array.isArray(test);
console.log(res); // выведзе false
Дадзены двухмерны масіў:
let test = [
[1, 2, 3],
{a: 1, b: 2, c: 3},
[3, 4, 5],
{x: 1, y: 2, z: 3},
];
Перабярыце гэты масіў цыклам і для кожнага элемента праверце, масіў ён ці не.