JS中,如何檢查對象是否為數(shù)組?
本文已經(jīng)過原作者 Guest Contributor 授權(quán)翻譯 。
簡介
在 JS 中使用數(shù)組是一種常見操作,有時在開發(fā)中,獲得一個需要作為數(shù)組的變量,但是我們不確定它是否是數(shù)組,那要怎么去判斷是否為數(shù)組呢?
JS 中的非原始數(shù)據(jù)類型都是對象(函數(shù)具有自己的類型,但它們也是對象)。因此,僅使用typeof運算符來判斷是不夠的:
- 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ù)進行比較。因此,如果我們將它與數(shù)組構(gòu)造函數(shù)進行比較,就會知道它是否是數(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 運算符instanceof運算符檢查是否在對象的原型鏈中找到構(gòu)造函數(shù)。
像typeof運算符一樣,它返回布爾值。要確定變量是否為數(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
我們不太可能使用這個方法,但是了解更多關(guān)于 JS 對象的知識是沒有壞處的
總結(jié)
在本文中,我們研究了 JS 中確定對象是否是數(shù)組的幾種方法。最簡單的方法是Array.isArray()方法,以后大部小伙伴可能就是用它了。
但是,我們還可以利用instanceof運算符和其他對象屬性來確定它是否為數(shù)組。
我是小智,我們下期見。
作者:Guest Contributor 譯者:前端小智 來源:medium
原文:https://stackabuse.com/javascript-check-if-object-is-array/
本文轉(zhuǎn)載自微信公眾號「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號。