2010-07-13 21 views
11

भविष्यवाणी करता है मेरे पास ऑब्जेक्ट्स की एक सरणी है। प्रत्येक ऑब्जेक्ट में, दूसरों के बीच, एक आईडी विशेषता है। मैं एक विशिष्ट आईडी के साथ वस्तु की सरणी में सूचकांक खोजना चाहता हूं। JQuery में ऐसा करने के लिए कोई सुरुचिपूर्ण और सरल तरीका है?jQuery: सरणी में तत्व का सूचकांक जहां

+0

बिल्कुल मैं जो भी ढूंढ रहा हूं। बहुत खराब कोई सुरुचिपूर्ण समाधान वास्तव में ... – epeleg

उत्तर

4

मामले में आपको jQuery का उपयोग करने के बजाय जावास्क्रिप्ट में for लूप का उपयोग करना चाहिए। देखें रास्ता 3 http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/ में

अपडेट किया गया: jQuery जावास्क्रिप्ट में लिखा है और यह जावास्क्रिप्ट में यह भी लिखा एक और कोड की तुलना में तेजी नहीं हो सकता। यदि आप डोम के साथ काम करते हैं तो jQuery बहुत अच्छा है, लेकिन यदि आप सरल जावास्क्रिप्ट सरणी या ऑब्जेक्ट्स के साथ काम कर रहे हैं तो वास्तव में मदद नहीं करता है।

for (var i=0, l = ar.length; i<l; i++) { 
    if (ar[i].ID === specificID) { 
     // i is the index. You can use it here directly or make a break 
     // and use i after the loop (variables in javascript declared 
     // in a block can be used anywhere in the same function) 
     break; 
    } 
} 
if (i<l) { 
    // i is the index 
} 

महत्वपूर्ण है कि आप कुछ सरल जावास्क्रिप्ट नियम धारण करना चाहिए:: हमेशा स्थानीय चर (मत भूलना var चर घोषणा से पहले) और कैश की घोषणा

कोड आप देख रहे हैं कुछ इस तरह हो सकता है किसी भी गुण या अनुक्रमणिका जो आप स्थानीय चर में एक से अधिक बार उपयोग करते हैं (जैसे ऊपर ar.length)। (उदाहरण के लिए देखें http://wiki.forum.nokia.com/index.php/JavaScript_Performance_Best_Practices)

3

वास्तव में सुरुचिपूर्ण नहीं, लेकिन एक प्यारा चाल:

var index = parseInt(
    $.map(array, function(i, o) { return o.id === target ? i : ''; }).join('') 
); 

jQuery उस तरह कार्यात्मक निर्माणों का एक बहुत नहीं है; लाइब्रेरी का दर्शन वास्तव में डोम wrangling के काम पर केंद्रित है। वे .reduce() फ़ंक्शन भी नहीं जोड़ेंगे क्योंकि कोई भी कारण नहीं सोच सकता कि यह कोर कार्यक्षमता के लिए उपयोगी होगा।

Underscore.js लाइब्रेरी में ऐसी कई सुविधाएं हैं, और यह jQuery के साथ "अच्छा खेलता है"।

+0

अच्छा, लेकिन अक्षम है क्योंकि यह सरणी में सभी तत्वों पर चला जाता है ... यदि आप उन पर जाते हैं तो आप एक रिवर्स हैश भी बना सकते हैं ताकि आप कई लोगों के लिए क्यू का जवाब दे सकें "specificID" एस। – epeleg

+0

@pepeleg अच्छी तरह से हाँ, स्पष्ट रूप से। – Pointy

5
 
See [`Array.filter`][1] to filter an array with a callback function. Each object in the array will be passed to the callback function one by one. The callback function must return `true` if the value is to be included, or false if not. 

    var matchingIDs = objects.filter(function(o) { 
     return o.ID == searchTerm; 
    }); 

All objects having the ID as searchTerm will be returned as an array to matchingIDs. Get the matching element from the first index (assuming ID is unique and there's only gonna be one) 

    matchingIDs[0]; 

    [1]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter 

अद्यतन:

चेकआउट findIndex

items.findIndex(function(item) { item.property == valueToSearch; }); 

ECMAScript 6. से findIndex के बाद से अभी तक सबसे ब्राउज़र पर उपलब्ध नहीं है, तो आप को बैकफ़िल सकता है यह इस implementation का उपयोग कर रहा है:

if (!Array.prototype.findIndex) { 
    Array.prototype.findIndex = function(predicate) { 
    if (this == null) { 
     throw new TypeError('Array.prototype.findIndex called on null or undefined'); 
    } 
    if (typeof predicate !== 'function') { 
     throw new TypeError('predicate must be a function'); 
    } 
    var list = Object(this); 
    var length = list.length >>> 0; 
    var thisArg = arguments[1]; 
    var value; 

    for (var i = 0; i < length; i++) { 
     value = list[i]; 
     if (predicate.call(thisArg, value, i, list)) { 
     return i; 
     } 
    } 
    return -1; 
    }; 
} 
+2

ओपी ने पूछा कि तत्व का सूचकांक तत्व को स्वयं कैसे प्राप्त करें। – epeleg

+0

@epeleg - मैंने ऐसा प्रश्न ठीक से नहीं पढ़ा। प्रत्येक वस्तु के माध्यम से लूपिंग और एक मैच मिलने पर तोड़ने का तरीका ओलेग के सुझाव के रूप में जाना है। – Anurag

0

जॉर्डर का उपयोग करें। http://github.com/danstocker/jorder

अपनी सरणी को जॉर्डर तालिका में फ़ीड करें, और 'आईडी' फ़ील्ड पर एक अनुक्रमणिका जोड़ें। , तो आप पूरी पंक्ति चाहते हैं

var arrayidx = table.index('id').lookup([{ ID: MyID }]); 

तो:

var table = jOrder(data) 
    .index('id', ['ID']); 

फिर, द्वारा एक तत्व की सरणी सूचकांक प्राप्त

var filtered = table.where([{ ID: MyID }]); 

देखा।

1

इसके लिए कोई अंतर्निहित विधियां नहीं हैं; [].indexOf() विधि एक विधेय नहीं ले करता है, तो आप कुछ कस्टम की जरूरत है:

function indexOf(array, predicate) 
{ 
    for (var i = 0, n = array.length; i != n; ++i) { 
     if (predicate(array[i])) { 
      return i; 
     } 
    } 
    return -1; 
} 

var index = indexOf(arr, function(item) { 
    return item.ID == 'foo'; 
}); 

फ़ंक्शन -1 अगर विधेय कभी नहीं एक truthy मूल्य अर्जित करता है।

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