Atšķiriet masīvus no objektiem JavaScript
Kā minēts iepriekš, operators typeof
neļauj atšķirt masīvus un objektus.
Tomēr dažreiz tas ir jāizdara.
Palīgā nāks funkcija Array.isArray():
console.log( Array.isArray([]) ); // izvadīs true
console.log( Array.isArray({}) ); // izvadīs false
Nepalaižot kodu, nosakiet, ko izvadīs ekrānā konsolē:
console.log( Array.isArray([1, 2, 3]) );
Neplaizot kodu, nosakiet, ko izvadīs ekrānā konsolē:
console.log( Array.isArray({x: 1, y: 2, z: 3}) );