2010-08-02 16 views

उत्तर

23

आप 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; 
    } 
}); 
+2

आप "वापसी झूठी" जोड़ना चाहते हैं; इसके अंत में यदि कथन है तो मिलान तत्व के बाद 'प्रत्येक' जारी नहीं होता है। (JQuery.each() में "झूठी वापसी;" एक नियमित जावास्क्रिप्ट पाश में "ब्रेक;" के बराबर है।) –

+3

क्या यह कम करने के बजाए लोअरकेस नहीं है? – Sarfraz

+0

@ जोर्डन और @ सर्फ्राज़: दोनों अच्छे अंक –

1

नहीं। आपको अपने डेटा के साथ बेझिझक करना होगा, मैं आम तौर पर आसान तुलना के लिए अपने सभी तारों को कम करता हूं। कस्टम तुलना फ़ंक्शन का उपयोग करने की भी संभावना है जो तुलनात्मक मामले को असंवेदनशील बनाने के लिए आवश्यक परिवर्तन करेगा।

1

सरणी के माध्यम से और प्रत्येक तत्व toLower और आप के लिए क्या देख रहे हैं toLower, लेकिन समय में उस बिंदु पर पाश, आप के रूप में अच्छी बस इसे बजाय inArray का उपयोग कर के तुलना कर सकते हैं सकता है()

1

ऐसा लगता है कि आपको इसका अपना समाधान लागू करना पड़ सकता है। Here jQuery पर कस्टम फ़ंक्शंस जोड़ने पर एक अच्छा लेख है। आपको केवल लूप के लिए एक कस्टम फ़ंक्शन लिखना होगा और फिर डेटा को सामान्यीकृत करना होगा।

4

@ ड्रू विल्स के लिए धन्यवाद।

मैं इस रूप में यह दुबारा लिखा:

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; 
} 
+1

यह मेरे लिए पूरी तरह से काम किया। – Alan

1

इन दिनों मैं इस तरह के कार्यों के लिए underscore का उपयोग करना पसंद:

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 
24

में मामला किसी को भी एक और अधिक एकीकृत दृष्टिकोण का उपयोग कर चाहता था:

(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

+1 बहुत उपयोगी है, और चयनित उत्तर होना चाहिए। –

+0

इसे jQuery एपीआई में जोड़ा जाना चाहिए, इसे प्रतिलिपि बनाना हर बार मुश्किल होगा, ठीक है – Bor

+0

यह शानदार है। आखिरी पंक्ति की शुरुआत में इसे सिर्फ सही घुंघराले ब्रेस की जरूरत है। – EthR

संबंधित मुद्दे