JavaScript判斷一個對象是否為數(shù)組的方法有哪些

這篇文章主要介紹了JavaScript判斷一個對象是否為數(shù)組的方法有哪些,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

發(fā)展壯大離不開廣大客戶長期以來的信賴與支持,我們將始終秉承“誠信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠服務(wù)每家企業(yè),認(rèn)真做好每個細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及成都宣傳片制作等,在成都網(wǎng)站建設(shè)成都營銷網(wǎng)站建設(shè)、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。

在 JS 中使用數(shù)組是一種常見操作,有時在開發(fā)中,獲得一個需要作為數(shù)組的變量,但是我們不確定它是否是數(shù)組,那要怎么去判斷是否為數(shù)組呢?

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ù)組?!鞠嚓P(guān)教程推薦:JavaScript視頻教程】

使用 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.prototypetoString()方法顯示對象的類型。

對象的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

我們不太可能使用這個方法,但是了解更多關(guān)于 JS 對象的知識是沒有壞處的

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“JavaScript判斷一個對象是否為數(shù)組的方法有哪些”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)頁題目:JavaScript判斷一個對象是否為數(shù)組的方法有哪些
文章地址:http://muchs.cn/article36/ghiesg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)、云服務(wù)器、品牌網(wǎng)站設(shè)計(jì)網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)