利用JS判斷元素是否為數(shù)組的方法示例
此處提供可供驗(yàn)證的數(shù)據(jù)類型
let a = [1,2,3,4,5,6]; let b = [ {name: '張飛', type: 'tank'}, {name: '關(guān)羽', type: 'soldier'}, {name: '劉備', type: 'shooter'}, ]; let c = 123; let d = 'www'; let e = {name: '安琪拉', type: 'mage'};
1.通過Array.isArray()
Array.isArray()能判斷一個(gè)元素是否為數(shù)組,如果是就返回true,否則就返回false
console.log(Array.isArray(a)); // true console.log(Array.isArray(b)); // true console.log(Array.isArray(c)); // false console.log(Array.isArray(d)); // false console.log(Array.isArray(e)); // false
2.通過instanceof判斷
instanceof運(yùn)算符用于檢測(cè)某個(gè)實(shí)例是否屬于某個(gè)對(duì)象原型鏈中
console.log(a instanceof Array); // true console.log(b instanceof Array); // true console.log(c instanceof Array); // false console.log(d instanceof Array); // false console.log(e instanceof Array); // false
還可以用于判斷對(duì)象
console.log(e instanceof Object); // true
判斷是否為數(shù)組就是檢測(cè)Arrray.prototype屬性是否存在于變量數(shù)組(a,b)的原型鏈上,顯然a,b為數(shù)組,擁有Arrray.prototype屬性,所以為true
3.通過對(duì)象構(gòu)造函數(shù)的constructor判斷
Obiect的每個(gè)實(shí)例都有構(gòu)造函數(shù)constructor,保存著創(chuàng)建每個(gè)對(duì)象的函數(shù)
console.log(a.constructor === Array); // true console.log(b.constructor === Array); // true
以下包含判斷其它的數(shù)據(jù)類型驗(yàn)證
console.log(c.constructor === Number); // true console.log(d.constructor === String); // true console.log(e.constructor === Object); // true
4.通過Object.prototype.toString.call()判斷
通過原型鏈查找調(diào)用
console.log(Object.prototype.toString.call(a) === '[object Array]'); // true console.log(Object.prototype.toString.call(b) === '[object Array]'); // true
以下包含判斷其它的數(shù)據(jù)類型驗(yàn)證
console.log(Object.prototype.toString.call(c) === '[object Number]'); // true console.log(Object.prototype.toString.call(d) === '[object String]'); // true console.log(Object.prototype.toString.call(e) === '[object Object]'); // true
5.通過對(duì)象原型鏈上的isPrototypeOf()判斷
Array.prototype屬性為Array的構(gòu)造函數(shù)原型,里面包含有一個(gè)方法 isPrototypeOf() 用于測(cè)試一個(gè)對(duì)象是否存在于;另一個(gè)對(duì)象的原型鏈上。
console.log(Array.prototype.isPrototypeOf(a)); // true console.log(Array.prototype.isPrototypeOf(b)); // true console.log(Array.prototype.isPrototypeOf(c)); // false console.log(Array.prototype.isPrototypeOf(d)); // false console.log(Array.prototype.isPrototypeOf(e)); // false
總結(jié)
到此這篇關(guān)于利用JS判斷元素是否為數(shù)組的文章就介紹到這了,更多相關(guān)JS判斷元素為數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JS判斷數(shù)組里是否有重復(fù)元素的方法小結(jié)
- JavaScript判斷數(shù)組是否包含指定元素的方法
- JS判斷元素是否在數(shù)組內(nèi)的實(shí)現(xiàn)代碼
- JS判斷數(shù)組是否包含某元素實(shí)現(xiàn)方法匯總
- JS實(shí)現(xiàn)判斷數(shù)組是否包含某個(gè)元素示例
- 判斷數(shù)組是否包含某個(gè)元素的js函數(shù)實(shí)現(xiàn)方法
- javascript 判斷數(shù)組是否已包含了某個(gè)元素的函數(shù)
- javascript如何判斷數(shù)組內(nèi)元素是否重復(fù)的方法集錦
- 利用JS十分鐘判斷數(shù)組中存在元素的多種方式
- JS判斷元素是否存在數(shù)組中的5種方式總結(jié)
相關(guān)文章
JavaScript函數(shù)聲明和函數(shù)表達(dá)式的區(qū)別
這篇文章主要介紹了JavaScript函數(shù)聲明和函數(shù)表達(dá)式的區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06跟我學(xué)習(xí)javascript的call(),apply(),bind()與回調(diào)
跟我學(xué)習(xí)javascript的call(),apply(),bind()與回調(diào),感興趣的小伙伴們可以參考一下2015-11-11Uni-app跨平臺(tái)開發(fā)應(yīng)用入門實(shí)戰(zhàn)
這篇文章主要為大家介紹了Uni-app跨平臺(tái)開發(fā)應(yīng)用入門實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片并兼容IE/FF火狐/谷歌等主流瀏覽器
js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片做了兼容IE,F(xiàn)F火狐,谷歌等主流瀏覽器,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-06-06javascript 復(fù)雜的嵌套環(huán)境中輸出單引號(hào)和雙引號(hào)
如果簡(jiǎn)單的嵌套一般都是外面用雙引號(hào),則里面用單引號(hào),反之亦同,如果特別負(fù)責(zé)的嵌套大家看下如下的方法。2009-05-05