How do I make Array.indexOf() case insensitive?
我正在为一个网站编写一段代码,该网站将在一个数组中有一个名称列表并选择一个随机名称,我想添加一个功能,允许用户从数组中添加或删除一个名称。我拥有所有这些特性,但是当删除一个名称时,用户必须键入该名称以匹配数组中的大小写。我试着去做,这样会不区分大小写,我做错了什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <html> <!--Other code uneeded for this question--> <p id="canidates"> </p> <body> <input type="text" id="delname" /><button onclick="delName()">Remove Name from List</button> //Array of names var names = []; //Other code uneeded for this question //List of Canidates document.getElementById('canidates').innerHTML = "List of Canidates:" + names.join(" |"); //Other code uneeded for this question //Remove name from Array function delName() { var dnameVal = document.getElementById('delname').value; var pos = names.indexOf(dnameVal); var namepos = names[pos] var posstr = namepos.toUpperCase(); var dup = dnameVal.toUpperCase(); if(dup != posstr) { alert("Not a valid name"); } else { names.splice(pos, 1); document.getElementById('canidates').innerHTML = "List of Canidates:" + names.join(" |"); } } </body> </html> |
ES2015查找索引:
1 2 3 4 5 | var array = ['I', 'hAve', 'theSe', 'ITEMs'], query = 'these', result = array.findIndex(item => query.toLowerCase() === item.toLowerCase()); console.log(result); // 2 |
在ECMA-262第5版中,您可以使用
1 2 3 4 5 6 7 8 9 10 | var array = [ 'I', 'hAve', 'theSe', 'ITEMs' ]; var query = 'these'.toLowerCase(); var index = -1; array.some(function(element, i) { if (query === element.toLowerCase()) { index = i; return true; } }); // Result: index = 2 |
简单的方法是使用一个临时数组,该数组包含所有大写名称。然后您可以比较用户输入。所以你的代码可能会变成这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function delName() { var dnameVal = document.getElementById('delname').value; var upperCaseNames = names.map(function(value) { return value.toUpperCase(); }); var pos = upperCaseNames.indexOf(dnameVal.toUpperCase()); if(pos === -1) { alert("Not a valid name"); } else { names.splice(pos, 1); document.getElementById('canidates').innerHTML = "List of Canidates:" + names.join(" |"); } } |
希望这有助于解决你的问题。
最优雅的解决方案是先将数组转换为字符串,然后进行不区分大小写的比较。例如:
1 2 3 4 5 6 7 8 9 | var needle ="PearS" var haystack = ["Apple","banNnas","pEArs"]; var stricmp = haystack.toString().toLowerCase(); // returns //"apple,bananas,pears" if (stricmp.indexOf(needle.toLowerCase()) > -1) { // the search term was found in the array } else { // the search term was not found in the array } |
最好创建自己的自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 'use strict'; var customIndexOf = function(arrayLike, searchElement) { var object = Object(arrayLike); var length = object.length >>> 0; var fromIndex = arguments.length > 2 ? arguments[2] >> 0 : 0; if (length < 1 || typeof searchElement !== 'string' || fromIndex >= length) { return -1; } if (fromIndex < 0) { fromIndex = Math.max(length - Math.abs(fromIndex), 0); } var search = searchElement.toLowerCase(); for (var index = fromIndex; index < length; index += 1) { if (index in object) { var item = object[index]; if (typeof item === 'string' && search === item.toLowerCase()) { return index; } } } return -1; }; var names = [ 'John', 'Anne', 'Brian' ]; console.log(customIndexOf(names, 'aNnE')); |
甚至
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 'use strict'; var customIndexOf = function(array, searchElement, fromIndex) { return array.map(function(value) { return value.toLowerCase(); }).indexOf(searchElement.toLowerCase(), fromIndex); }; var names = [ 'John', 'Anne', 'Brian' ]; console.log(customIndexOf(names, 'aNnE')); |
您可能还需要添加更多的检查,以确保数组中的每个元素实际上是一个
使用地图法是可能的。例如,请参见下面的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 | var _name = ['prasho','abraham','sam','anna'] var _list = [{name:'prasho'},{name:'Gorge'}]; for(var i=0;i<_list.length;i++) { if(_name.map(function (c) { return c.toLowerCase(); }).indexOf(_list[i].name.toLowerCase()) != -1) { //do what ever }else{ //do what ever } } |
更多信息
我需要类似的东西,在这里我需要使用
1 2 3 | function compare(l1: string, l2: string, ignoreCase = true): boolean { return (ignoreCase ? l1.toLowerCase() : l1).includes((ignoreCase ? l2.toLowerCase() : l2)); } |
同样的原理也适用于
1 2 3 | function indexOf(l1: string, l2: string, ignoreCase = true): number { return (ignoreCase ? l1.toLowerCase() : l1).indexOf((ignoreCase ? l2.toLowerCase() : l2)); } |
我知道这不是具体的
不过,要回答ops问题,您可以将其类似地应用于与@ulit jaidee的答案组合在一起的数组(对这一点的微小更改是,如果数组值中包含空格,则使用tilda字符作为分隔符)。
1 2 3 | function compare(l1: any[], l2: any[], ignoreCase = true): boolean { return (ignoreCase ? l1.join('~').toLowerCase().split('~') : l1).indexOf((ignoreCase ? l2.join('~').toLowerCase().split('~') : l2)); } |
再次希望这有帮助。
这是最短的一个。
1 | haystack.join(' ').toLowerCase().split(' ').indexOf(needle.toLowerCase()) |
将数组转换为由分隔符分隔的字符串,将该字符串转换为小写,然后使用相同的分隔符将字符串重新拆分为数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function findIt(arr, find, del) { if (!del) { del = '_//_'; } arr = arr.join(del).toLowerCase().split(del); return arr.indexOf(find.toLowerCase()); } var arr = ['Tom Riddle', 'Ron Weasley', 'Harry Potter', 'Hermione Granger']; var find = 'HaRrY PoTtEr'; var index = findIt(arr, find); if (~index) { alert('Found ' + arr[index] + '! :D'); } else { alert('Did not find it. D:'); } |
你不能让它不区分大小写。我会用一个对象来保存一组名称:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | function Names() { this.names = {}; this.getKey = function(name) { return name.toLowerCase(); } this.add = function(name) { this.names[this.getKey(name)] = name; } this.remove = function(name) { var key = this.getKey(name); if (key in this.names) { delete this.names[key]; } else { throw Error('Name does not exist'); } } this.toString = function() { var names = []; for (var key in this.names) { names.push(this.names[key]); } return names.join(' | '); } } var names = new Names(); function update() { document.getElementById('canidates').innerHTML = 'List of Canidates: ' + names; } function deleteName() { var name = document.getElementById('delname').value; try { names.remove(name); update(); } catch { alert('Not a valid name'); } } update(); |