簡體   English   中英

如何使indexof()區分數組中的兩個相似數字

[英]How to make indexof() differentiate between two like numbers in an array

我有一個數組說

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

我正在使用javascript indexof()檢查數組中是否存在元素,以及是否不退出以執行某項操作。

我的問題是,當數組中不存在像1,2,3,4,5,6之類的元素時,它仍會判斷像10代表1、12代表2、2,13代表3、14代表4,15之類的元素5、16、6。因此即使元素不存在,它仍會返回該元素存在

例如:

if x = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

這仍然無法正常工作,因為它將10視為1

 if(data1.indexOf(1)===-1){ document.getElementById("s1").style.backgroundColor= "red"; $("input[name='seat1']").attr("disabled", "disabled"); } 

您可以parse字符串並將indexOf用作數組方法。

JSON.parse(data1).indexOf(1) ...

Array.indexOf可在任何瀏覽器中工作> IE8: MDN瀏覽器兼容性

如果需要任何支持,可以使用Polyfill。

 // POLYFILL TO SUPPORT ANY BROWSER // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill // Production steps of ECMA-262, Edition 5, 15.4.4.14 // Reference: http://es5.github.io/#x15.4.4.14 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(searchElement, fromIndex) { var k; // 1. Let o be the result of calling ToObject passing // the this value as the argument. if (this == null) { throw new TypeError('"this" is null or not defined'); } var o = Object(this); // 2. Let lenValue be the result of calling the Get // internal method of o with the argument "length". // 3. Let len be ToUint32(lenValue). var len = o.length >>> 0; // 4. If len is 0, return -1. if (len === 0) { return -1; } // 5. If argument fromIndex was passed let n be // ToInteger(fromIndex); else let n be 0. var n = +fromIndex || 0; if (Math.abs(n) === Infinity) { n = 0; } // 6. If n >= len, return -1. if (n >= len) { return -1; } // 7. If n >= 0, then Let k be n. // 8. Else, n<0, Let k be len - abs(n). // If k is less than 0, then let k be 0. k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); // 9. Repeat, while k < len while (k < len) { // a. Let Pk be ToString(k). // This is implicit for LHS operands of the in operator // b. Let kPresent be the result of calling the // HasProperty internal method of o with argument Pk. // This step can be combined with c // c. If kPresent is true, then // i. Let elementK be the result of calling the Get // internal method of o with the argument ToString(k). // ii. Let same be the result of applying the // Strict Equality Comparison Algorithm to // searchElement and elementK. // iii. If same is true, return k. if (k in o && o[k] === searchElement) { return k; } k++; } return -1; }; } // TEST ARRAY var arr = [0,1,2,3,4,6,7,8,9,10,12,13,14,16,17,18,20] // TESTS var offset = 0; for(var i = 0; i <= 20; i++) { if(arr.indexOf(i) === -1) { console.log(i, "not found"); } else { console.log(i, "found") } } 

我會把它們轉換成這樣的字符串。

 function existsInArray(numberArrays, number){ const numAsStrings = numberArrays.join(',').split(',') return numAsStrings.indexOf(String(number)) > -1 } console.log(existsInArray([1,2,3,4,5,6,33], 33)) //true console.log(existsInArray([1,2,3,4,5,6,33], 34)) //false 

希望這可以幫助。

您的示例代碼片段沒有幫助,因為它不會顯示您正在處理的數據(“ data1”)。

您描述的行為不是您的示例數組所獲得的:

 x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; alert("indexOf(1):" + x.indexOf(1)); alert("indexOf(10):" + x.indexOf(10)); alert("indexOf(0):" + x.indexOf(0)); 

它按預期工作。

您的真實數據可能包含字符串,而不是整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM