In questo esempio imparerai a scrivere un programma JavaScript che controllerà se un oggetto è un array.
Per comprendere questo esempio, è necessario conoscere i seguenti argomenti di programmazione JavaScript:
- Javascript Array isArray ()
- Funzione JavaScript ed espressioni di funzione
- JavaScript Array
Esempio: controllare l'array utilizzando Array.isArray ()
// program to check if an object is an array function checkObject(arr) ( // check if arr is array const result = Array.isArray(arr); if(result) ( console.log(`($(arr)) is an array.`); ) else ( console.log(`$(arr) is not an array.`); ) ) const array = (1, 2, 3); // call the function checkObject(array);
Produzione
(1,2,3) è un array.
Nel programma precedente, il Array.isArray()
metodo viene utilizzato per verificare se un oggetto è un array.
Il Array.isArray()
metodo restituisce true
se un oggetto è un array, altrimenti restituisce false
.
Nota : per un array, l' typeof
operatore restituisce un oggetto.
Per esempio,
const arr = (1, 2, 3); console.log(typeof arr); // object