js判断一个值是否存在于一个js数组中 js判断一个值是否存在于一个js数组中
js\u5982\u4f55\u67e5\u627e\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u503c\u53ef\u4ee5\u7528\u6570\u7ec4\u7684includes\u51fd\u6570\u5224\u65ad\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u503c\u3002
1\u3001\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e3a\u6570\u7ec4\u6dfb\u52a0\u6210\u5458\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u6570\u7ec4\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u8fd9\u91cc\u4ee5arr\u4e3a\u4f8b\uff1a
2\u3001\u4f7f\u7528\u6570\u7ec4\u7684includes\u51fd\u6570\uff0c\u51fd\u6570\u7684\u53c2\u6570\u4e3a\u67e5\u627e\u7684\u503c\uff0c\u5982\u679c\u6570\u7ec4\u4e2d\u6709\u88ab\u67e5\u627e\u7684\u503c\uff0c\u5219\u51fd\u6570\u8f93\u51fa\u201ctrue\u201d\uff1a
3\u3001\u5982\u679c\u6570\u7ec4\u4e2d\u6ca1\u6709\u88ab\u67e5\u627e\u7684\u503c\uff0c\u5219\u51fd\u6570\u4f1a\u8f93\u51fa\u201cfalse\u201d\uff1a
js\u4e2d\u5224\u65ad\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a js\u6570\u7ec4\u4e2d\uff0c\u76f8\u5f53\u4e8e php \u8bed\u8a00\u4e2d\u7684 in_array \u51fd\u6570\u3002
Array.prototype.S=String.fromCharCode(2);
Array.prototype.in_array=function(e){
var r=new RegExp(this.S+e+this.S);
return (r.test(this.S+this.join(this.S)+this.S));};\u7528\u6cd5\u5982\u4e0b\uff1a
var arr=new Array(["b",2,"a",4,"test"]);
arr.in_array('test');//\u5224\u65ad test \u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e arr \u6570\u7ec4\u4e2d\uff0c\u5b58\u5728\u8fd4\u56detrue \u5426\u5219false\uff0c\u6b64\u5904\u5c06\u8fd4\u56detrue\u6ce8\uff1a\u6b64\u51fd\u6570\u53ea\u5bf9\u5b57\u7b26\u548c\u6570\u5b57\u6709\u6548\u3002
\u53e6\u5916\u5728jquery\u4e2d\u4e5f\u6709\u4e00\u4e2ain_array\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7684\u539f\u578b\u4e3a\uff1a
\u5b9a\u4e49\uff1a\u786e\u5b9a\u7b2c\u4e00\u4e2a\u53c2\u6570value\u5728\u7ed9\u5b9a\u7684\u6570\u7ec4array\u4e2d\u7684\u4f4d\u7f6e(\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de -1 )\u3002
绛旓細= -1){ if ((a*i)+(a*j)==(a*k)) { return true; } else { return false; }}
绛旓細js涓浣鍒ゆ柇涓涓瀛楃涓插湪鍙︿竴涓瓧绗︿覆鐨勪綅缃紝鍙互鐩存帴璋冪敤indexOf鏂规硶鍗冲彲锛屽鏋滀笉瀛樺湪鍒欎細杩斿洖-1銆備緥濡傦細var a1="abcdefg"; var a2="ef"; alert("瀛楃涓瞐1:"+a1); alert("瀛楃涓瞐2:"+a2); alert("瀛楃涓瞐2鍦ㄥ瓧绗︿覆a1鐨勪綅缃細"+a1.indexOf(a2));...
绛旓細鎴戝彧鐭ラ亾涓绉嶏紝灏辨槸閫氳繃寰幆鏉ワ紝鐒跺悗鐢涓涓閿兼暟缁勬潵鏉ュ瓨鍌ㄥ肩殑鍑虹幇娆℃暟锛屽涓婇潰閭d釜鏁扮粍鐨勫彉閲忓悕涓簐ar jsArr锛岄偅涔堝彲浠ヤ笅闈㈢殑鏂规硶鏉ヨ褰曘倂ar tmp = new Array();for(var i = 0, j = jsArr.length; i < j; i++) { var v = jsArr[i];tmp[v] = (typeof(tmp[v]) != 'undefin...
绛旓細鏃犻渶鎬濊冿紝鎴戜滑鍙互寰楀埌 O(n^2) 澶嶆潅搴︾殑瑙f硶銆傚畾涔涓涓鍙橀噺鏁扮粍 res 淇濆瓨缁撴灉锛岄亶鍘嗛渶瑕佸幓閲嶇殑鏁扮粍锛屽鏋滆鍏冪礌宸茬粡瀛樺湪鍦 res 涓簡锛屽垯璇存槑鏄噸澶嶇殑鍏冪礌锛屽鏋滄病鏈夛紝鍒欐斁鍏 res 涓俧unction unique(a) { var res = [];for (var i = 0, len = a.length; i < len; i++) { var i...
绛旓細type="text/javascript"> var str="123,321,12323,123";var num=str.split(",");var result="鐩稿悓鐨勬暟鏈夛細";for(i=0;i<num.length;i++){ for(j=i+1;j<num.length;j++){ if(num[i]==num[j])result+=num[i]+"";} } document.write(result); ...
绛旓細杩欓噷灏 js 濡備綍瀹炵幇鏁扮粍鍘婚噸鏁寸悊鍑5绉嶆柟娉曪紝骞堕檮涓婃紨绀篋emo 浠ュ強 婧愮爜銆1.閬嶅巻鏁扮粍娉 鏈绠鍗曠殑鍘婚噸鏂规硶锛屽疄鐜版濊矾锛氭柊寤轰竴鏂版暟缁勶紝閬嶅巻浼犲叆鏁扮粍锛屽间笉鍦ㄦ柊鏁扮粍灏卞姞鍏ヨ鏂版暟缁勪腑锛涙敞鎰忕偣锛鍒ゆ柇鍊兼槸鍚﹀湪鏁扮粍鐨勬柟娉曗渋ndexOf鈥濇槸ECMAScript5 鏂规硶锛孖E8浠ヤ笅涓嶆敮鎸侊紝闇澶氬啓涓浜涘吋瀹逛綆鐗堟湰娴忚鍣ㄤ唬鐮侊紝婧愮爜...
绛旓細1銆侀鍏堬紝鎵撳紑html缂栬緫鍣紝鏂板缓html鏂囦欢锛屼緥濡傦細index.html銆2銆佸湪index.html鐨勬爣绛句腑锛岃緭鍏js浠g爜锛歷ar a = [1, 2, 5, 7, 8], b = [2, 4, 6, 8];var c = [];for (var i=0; i
绛旓細绗竴绉嶅疄鐜版柟寮忥紝鐩存帴鐢ㄢ,鈥濇媶鍒嗗瓧涓蹭负鏁扮粍锛岀敤for寰幆渚濇鍒ゆ柇姣忎釜瀛楃鏄惁鏈夐噸澶嶏細 function CheckRepeat(){ var data = document.getElementById("txtData").value; var r = getRepeatData(data); alert(r); } function getRepeatData(input){ var ary = input...
绛旓細鏁扮粍 arguments,js 浼犻掑弬鏁版椂浼氭妸鍙傛暟淇濆瓨鍒 arguments 閲岄潰 function isEquals(){ var i,j;if(arguments.length < 2){ return '鑷冲皯闇瑕佷袱涓瓧绗︿覆';} for(i = 0;i < arguments.length;i++){ for(j = i + 1;j < arguments.length;j++){ if(arguments[i]== arguments[j]){ retur...
绛旓細姝e垯琛ㄨ揪寮忓涓嬶細^[A-Z](,[A-Z])js绀轰緥濡備笅锛歷ar a = /^[A-Z](,[A-Z])*$/g;alert(a.test('A,F,C,C,Z'));//true alert(a.test('B,C,'));//false alert(a.test(',X,Z'));//false