简体   繁体   English

如何使indexof()区分数组中的两个相似数字

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

I have an array say 我有一个数组说

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

I am using javascript indexof() to check if an element exist or not in the array and if it doesn't exits to do something.... 我正在使用javascript indexof()检查数组中是否存在元素,以及是否不退出以执行某项操作。

my problem is that when an element like, 1,2,3,4,5,6 doesn't exist in the array, it still judge elements like 10 for 1, 12 for 2, 13 for 3, 14 for 4, 15 for 5, 16 for 6. and so it will still return that the element exists eventhough it is not there 我的问题是,当数组中不存在像1,2,3,4,5,6之类的元素时,它仍会判断像10代表1、12代表2、2,13代表3、14代表4,15之类的元素5、16、6。因此即使元素不存在,它仍会返回该元素存在

eg: 例如:

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

this will still not work as it will see 10 as 1 这仍然无法正常工作,因为它将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 works in any browser > IE8: MDN Browser compatibility Array.indexOf可在任何浏览器中工作> IE8: MDN浏览器兼容性

Polyfill It if you need to support anything. 如果需要任何支持,可以使用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") } } 

I would convert them to strings like this. 我会把它们转换成这样的字符串。

 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 

Hope this helps. 希望这可以帮助。

Your example snippet is not helpful, as it does not show the data ("data1") you are working on. 您的示例代码片段没有帮助,因为它不会显示您正在处理的数据(“ data1”)。

The behaviour you describe is not the one which one gets with your example array: 您描述的行为不是您的示例数组所获得的:

 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)); 

It works as intended. 它按预期工作。

Your real data proably contains strings and not integers. 您的真实数据可能包含字符串,而不是整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM