溫馨提示×

溫馨提示×

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

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

es6如何判斷是否是數(shù)組

發(fā)布時間:2022-05-10 14:28:01 來源:億速云 閱讀:1385 作者:iii 欄目:web開發(fā)

這篇文章主要講解了“es6如何判斷是否是數(shù)組”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“es6如何判斷是否是數(shù)組”吧!

3種判斷方法:1、使用“Array.isArray(數(shù)組對象)”語句來判斷,如果是數(shù)組則返回true。2、使用“數(shù)組對象.constructor===Array”語句來判斷。3、使用“數(shù)組對象 instanceof Array”語句來判斷。

本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 6版、Dell G3電腦。

es6判斷是否是數(shù)組的方法:

方法1:使用isArray()方法

isArray() 方法用于判斷一個對象是否為數(shù)組。

如果對象是數(shù)組返回 true,否則返回 false。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(Array.isArray(fruits));
if(Array.isArray(fruits)){
	console.log("是數(shù)組");
}else{
	console.log("不是數(shù)組");
}

es6如何判斷是否是數(shù)組

方法2:利用constructor 屬性

利用數(shù)組對象.constructor === Array 語句,如果是數(shù)組返回 true,否則返回 false。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.constructor === Array);
if(fruits.constructor === Array){
	console.log("是數(shù)組");
}else{
	console.log("不是數(shù)組");
}

es6如何判斷是否是數(shù)組

方法3:利用instanceof運算符

instanceof 運算符用于檢測構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在某個實例對象的原型鏈上

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits instanceof Array);
if(fruits instanceof Array){
	console.log("是數(shù)組");
}else{
	console.log("不是數(shù)組");
}

es6如何判斷是否是數(shù)組

感謝各位的閱讀,以上就是“es6如何判斷是否是數(shù)組”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對es6如何判斷是否是數(shù)組這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

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

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

es6
AI