溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript中怎么檢查對象是否為數(shù)組

發(fā)布時間:2021-08-02 15:09:25 來源:億速云 閱讀:105 作者:Leah 欄目:web開發(fā)

JavaScript中怎么檢查對象是否為數(shù)組,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

JS 中的非原始數(shù)據(jù)類型都是對象(函數(shù)具有自己的類型,但它們也是對象)。因此,僅使用typeof運(yùn)算符來判斷是不夠的:

let result = { subject: 'Science', marks: 97 }; let numbers = [1, 2, 3, 4, 5];  console.log(typeof result); // Object console.log(typeof numbers); // Object

在本文中,我們來研究如何在 JS 中檢查給定變量或值是否為數(shù)組。

使用 Array.isArray() 方法

顧名思義,此方法可用于識別給定參數(shù)是否為數(shù)組,它返回一個布爾值(true/false)和結(jié)果。

例如,使用以下變量,Array.isArray()方法可以正確判斷是否為數(shù)組:

let result = { subject: "Science", marks: 97 }; // Object let numbers = [1, 2, 3, 4, 5]; // Array let name = "Mark"; // String let names = new Array("Jill", "Jane", "Jacqueline");  console.log(Array.isArray(result)); // false console.log(Array.isArray(numbers)); // true console.log(Array.isArray(name)); // false console.log(Array.isArray(names)); // true

使用對象的構(gòu)造函數(shù)屬性

每個對象都有一個constructor  屬性(除了使用object.create(null)創(chuàng)建的對象,這種情況不太可能出現(xiàn))。我們可以直接將constructor 屬性與 JS  的構(gòu)造函數(shù)進(jìn)行比較。因此,如果我們將它與數(shù)組構(gòu)造函數(shù)進(jìn)行比較,就會知道它是否是數(shù)組。

注意:構(gòu)造函數(shù)是用來初始化對象的函數(shù)。如果使用new關(guān)鍵字創(chuàng)建了一個對象,那么使用的是構(gòu)造函數(shù)。例如,在let myArray = new  Array(1,2)中,使用的構(gòu)造函數(shù)是Array()。

可以使用constructor 屬性來確定變量是否是數(shù)組:

let result = { subject: "Science", marks: 97 }; let numbers = [1, 2, 3, 4, 5]; let name = "Mark"; let names = new Array("小智", "小力", "小吳");  console.log(result.constructor === Array); // false console.log(numbers.constructor === Array); // true console.log(name.constructor === Array); // false console.log(names.constructor === Array); // true

使用 instanceof 運(yùn)算符instanceof運(yùn)算符檢查是否在對象的原型鏈中找到構(gòu)造函數(shù)。

像typeof運(yùn)算符一樣,它返回布爾值。要確定變量是否為數(shù)組,可以使用instanceof,如下所示:

let result = { subject: "Science", marks: 97 }; let numbers = [1, 2, 3, 4, 5]; let name = "Mark"; let names = new Array("小智", "小力", "小吳");  console.log(result instanceof Array); // false console.log(numbers instanceof Array); // true console.log(name instanceof Array); // false console.log(names instanceof Array); // true

使用 Object.prototype.call() 方法

JS  中的所有對象均從主原型對象繼承屬性,該對象命名為Object.prototype。Object.prototype中存在toString()方法,這是每個對象都有自己的toString()方法的原因,  Object.prototype的 toString()方法顯示對象的類型。

對象的call()方法執(zhí)行一個函數(shù),但將this 值更改為傳入?yún)?shù)的對象,例如,它允許一個對象使用另一個對象的方法。

因此,我們可以使用Object.prototype.toString()來打印類型,然后使用call()來處理另一個對象,然后比較這個字符串值以確定它是否是一個數(shù)組。

let result = { subject: "Science", marks: 97 }; let numbers = [1, 2, 3, 4, 5]; let name = "Mark"; let names = new Array("小智", "小力", "小吳");  console.log(Object.prototype.toString.call(result)); // [object Object] console.log(Object.prototype.toString.call(numbers)); // [object Array] console.log(Object.prototype.toString.call(name)); // [object String] console.log(Object.prototype.toString.call(names)); // [object Array]  console.log(Object.prototype.toString.call(result) === "[object Array]"); // false console.log(Object.prototype.toString.call(numbers) === "[object Array]"); // true console.log(Object.prototype.toString.call(name) === "[object Array]"); // false console.log(Object.prototype.toString.call(names) === "[object Array]"); // true

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI