Il titolo lo riassume.Un modo per rendere jQuery.inArray() case sensitive?
risposta
È possibile utilizzare each() ...
// Iterate over an array of strings, select the first elements that
// equalsIgnoreCase the 'matchString' value
var matchString = "MATCHME".toLowerCase();
var rslt = null;
$.each(['foo', 'bar', 'matchme'], function(index, value) {
if (rslt == null && value.toLowerCase() === matchString) {
rslt = index;
return false;
}
});
No. Dovrai armeggiare con i tuoi dati, di solito faccio tutte le mie stringhe in minuscolo per facili confronti. Esiste anche la possibilità di utilizzare una funzione di confronto personalizzata che effettuerebbe le trasformazioni necessarie per rendere insensibile la comparazione del caso.
potrebbe scorrere l'array e tolower ogni elemento e ToLower quello che stai cercando, ma a quel punto nel tempo, si può anche semplicemente confrontarlo invece di utilizzare inArray()
Sembra che potrebbe essere necessario implementare la propria soluzione a questo. Here è un buon articolo sull'aggiunta di funzioni personalizzate a jQuery. Dovrai semplicemente scrivere una funzione personalizzata per eseguire il ciclo e normalizzare i dati, quindi confrontare.
Grazie a @Drew Wills.
ho riscritto come questo:
function inArrayCaseInsensitive(needle, haystackArray){
//Iterates over an array of items to return the index of the first item that matches the provided val ('needle') in a case-insensitive way. Returns -1 if no match found.
var defaultResult = -1;
var result = defaultResult;
$.each(haystackArray, function(index, value) {
if (result == defaultResult && value.toLowerCase() == needle.toLowerCase()) {
result = index;
}
});
return result;
}
Questo ha funzionato perfettamente per me. – Alan
In questi giorni io preferisco usare underscore per compiti come questo:
a = ["Foo","Foo","Bar","Foo"];
var caseInsensitiveStringInArray = function(arr, val) {
return _.contains(_.map(arr,function(v){
return v.toLowerCase();
}) , val.toLowerCase());
}
caseInsensitiveStringInArray(a, "BAR"); // true
Nel caso qualcuno voleva un approccio più integrato con jquery:
(function($){
$.extend({
// Case insensative $.inArray (http://api.jquery.com/jquery.inarray/)
// $.inArrayIn(value, array [, fromIndex])
// value (type: String)
// The value to search for
// array (type: Array)
// An array through which to search.
// fromIndex (type: Number)
// The index of the array at which to begin the search.
// The default is 0, which will search the whole array.
inArrayIn: function(elem, arr, i){
// not looking for a string anyways, use default method
if (typeof elem !== 'string'){
return $.inArray.apply(this, arguments);
}
// confirm array is populated
if (arr){
var len = arr.length;
i = i ? (i < 0 ? Math.max(0, len + i) : i) : 0;
elem = elem.toLowerCase();
for (; i < len; i++){
if (i in arr && arr[i].toLowerCase() == elem){
return i;
}
}
}
// stick with inArray/indexOf and return -1 on no match
return -1;
}
});
})(jQuery);
- 1. Posso rendere un Apache in esecuzione su Windows case-sensitive?
- 2. Is: text case sensitive?
- 3. Rendi mysql case sensitive?
- 4. CMake nomi variabili case sensitive?
- 5. case sensitive su Mac per file_exists()?
- 6. PHP/MySQL Ricerca - Case sensitive
- 7. MySQL regex query case sensitive
- 8. AngularJS + Base Href Case sensitive?
- 9. grep case sensitive [A-Z]?
- 10. Come creare un case in-sensitive TEqualityComparer per TDictionary?
- 11. Primavera 3.0 MVC vincolante Enums Case Sensitive
- 12. GSON non-Case Sensitive Enum deserializzazione
- 13. Gli ETAG HTTP devono essere case sensitive?
- 14. Variabili di Bash: case sensitive o no?
- 15. Come scrivere Case Sensitive Query per MS Access?
- 16. Problema con JPA, EclipseLink e case sensitive mysql
- 17. MySQL Case Insensitive ma Accent Sensitive UTF8 Chiave unica
- 18. Le intestazioni HTTP (Content-Type, & c.) Sono case-sensitive?
- 19. Il file Java esiste Case sensitive .jpg e .JPG
- 20. Le parole chiave di PHP sono case sensitive?
- 21. Come eseguire una ricerca case-sensitive su Emacs?
- 22. Devo usare jQuery.inArray()?
- 23. Sublime Text 2 - Trova e sostituisci: "case-sensitive" vs "preserve case"?
- 24. Come posso ottenere un percorso case sensitive su Linux senza l'iterazione della directory?
- 25. jQuery.inArray() non funziona come previsto
- 26. Simulazione sensibilità caso su un filesystem che non è case sensitive
- 27. Che cos'è "Scegli un numero o applica il filtro (formato: [groupId:] arttifactId, case sensitive contains" "
- 28. Un modo per rendere md5_file() più veloce?
- 29. Case-insensitive sostituzione PowerShell
- 30. SOLR Cerca query case sensitiveness
Si vorrà aggiungere un "return false;" alla fine di tale istruzione if in modo che 'ciascuno' non continui dopo che viene trovato un elemento corrispondente. (In jQuery.each() "return false;" è equivalente a "break;" in un normale ciclo di JavaScript.) –
Non è toLowerCase anziché aLower? – Sarfraz
@Jordan e @Sarfraz: entrambi i punti positivi –