JS中,如何檢查對象是否爲數組?

做者:Guest Contributor
譯者:前端小智
來源:medium
點贊再看,微信搜索 大遷世界,B站關注 前端小智這個沒有大廠背景,但有着一股向上積極心態人。本文 GitHub https://github.com/qq44924588... 上已經收錄,文章的已分類,也整理了不少個人文檔,和教程資料。

最近開源了一個 Vue 組件,還不夠完善,歡迎你們來一塊兒完善它,也但願你們能給個 star 支持一下,謝謝各位了。javascript

github 地址:https://github.com/qq44924588...前端

簡介

在 JS 中使用數組是一種常見操做,有時在開發中,得到一個須要做爲數組的變量,可是咱們不肯定它是不是數組,那要怎麼去判斷是否爲數組呢?vue

JS 中的非原始數據類型都是對象(函數具備本身的類型,但它們也是對象)。 所以,僅使用typeof運算符來判斷是不夠的:java

let result = { subject: 'Science', marks: 97 };
let numbers = [1, 2, 3, 4, 5];

console.log(typeof result); // Object
console.log(typeof numbers); // Object

在本文中,咱們來研究如何在 JS 中檢查給定變量或值是否爲數組。git

使用 Array.isArray() 方法

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

例如,使用如下變量,Array.isArray()方法能夠正確判斷是否爲數組:面試

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

使用對象的構造函數屬性

每一個對象都有一個constructor 屬性(除了使用object.create(null)建立的對象,這種狀況不太可能出現)。咱們能夠直接將constructor 屬性與 JS 的構造函數進行比較。所以,若是咱們將它與數組構造函數進行比較,就會知道它是不是數組。數組

注意:構造函數是用來初始化對象的函數。若是使用new關鍵字建立了一個對象,那麼使用的是構造函數。例如,在let myArray = new Array(1,2)中,使用的構造函數是Array()微信

可使用constructor 屬性來肯定變量是不是數組:函數

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運算符檢查是否在對象的原型鏈中找到構造函數。

typeof運算符同樣,它返回布爾值。 要肯定變量是否爲數組,可使用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.prototypeObject.prototype中存在toString()方法,這是每一個對象都有本身的toString()方法的緣由, Object.prototypetoString()方法顯示對象的類型。

對象的call()方法執行一個函數,但將this 值更改成傳入參數的對象,例如,它容許一個對象使用另外一個對象的方法。

所以,咱們可使用Object.prototype.toString()來打印類型,而後使用call()來處理另外一個對象,而後比較這個字符串值以肯定它是不是一個數組。

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

咱們不太可能使用這個方法,可是瞭解更多關於 JS 對象的知識是沒有壞處的

總結

在本文中,咱們研究了 JS 中肯定對象是不是數組的幾種方法。最簡單的方法是Array.isArray()方法,之後大部小夥伴可能就是用它了。

可是,咱們還能夠利用instanceof運算符和其餘對象屬性來肯定它是否爲數組。

我是小智,咱們下期見。


代碼部署後可能存在的BUG無法實時知道,過後爲了解決這些BUG,花了大量的時間進行log 調試,這邊順便給你們推薦一個好用的BUG監控工具 Fundebug

原文:https://stackabuse.com/javasc...

交流

文章每週持續更新,能夠微信搜索「 大遷世界 」第一時間閱讀和催更(比博客早一到兩篇喲),本文 GitHub https://github.com/qq449245884/xiaozhi 已經收錄,整理了不少個人文檔,歡迎Star和完善,你們面試能夠參照考點複習,另外關注公衆號,後臺回覆福利,便可看到福利,你懂的。

本文同步分享在 博客「前端小智」(SegmentFault)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索