JavaScript判斷數(shù)組是否包含指定元素的方法
更新時間:2015年07月01日 12:14:40 作者:不吃皮蛋
這篇文章主要介紹了JavaScript判斷數(shù)組是否包含指定元素的方法,涉及javascript中contains方法的使用技巧,需要的朋友可以參考下
本文實例講述了JavaScript判斷數(shù)組是否包含指定元素的方法。分享給大家供大家參考。具體如下:
這段代碼通過prototype定義了數(shù)組方法,這樣就可以在任意數(shù)組調(diào)用contains方法
/** * Array.prototype.[method name] allows you to define/overwrite an objects method * needle is the item you are searching for * this is a special variable that refers to "this" instance of an Array. * returns true if needle is in the array, and false otherwise */ Array.prototype.contains = function ( needle ) { for (i in this) { if (this[i] == needle) return true; } return false; }
用法:
// Now you can do things like: var x = Array(); if (x.contains('foo')) { // do something special }
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
JavaScript中檢測數(shù)據(jù)類型的四種方法總結(jié)
這篇文章主要為大家詳細介紹了四個JavaScript中檢測數(shù)據(jù)類型的常用方法,文中的示例代碼講解詳細,具有一定的參考價值,需要的可以參考一下2023-04-04javascript跟隨滾動效果插件代碼(javascript Follow Plugin)
這篇文章介紹了javascript跟隨滾動效果插件代碼(javascript Follow Plugin),有需要的朋友可以參考一下2013-08-08DLL+ ActiveX控件+WEB頁面調(diào)用例子
因項目需要,開始學習并研究VC、DLL及ActiveX控件,網(wǎng)上資料找了很多,但沒一個可用的或者說沒一個例子可理解并運行的。沒辦法,自己研究吧。功夫不負有心人,終有小成了,呵呵,現(xiàn)在把自己學習總結(jié)了一下,獻給需要的人。2010-08-08