2012-03-09 10 views
8

Utilizzando jQuery, come faccio a scorrere una serie di oggetti e restituire quello che soddisfa determinati criteri?jQuery: come trovare un oggetto con una certa proprietà è uguale a un certo valore?

+1

Questo dipende interamente dagli oggetti e dalla condizione. Abbiamo bisogno di alcuni dati di esempio. –

+0

Cosa stai cercando di fare? Puoi pubblicare qualche codice> – elclanrs

+0

Ogni oggetto ha proprietà ID e Amount. Una matrice contiene una dozzina di oggetti. – David

risposta

14

È possibile utilizzare la funzione jQuery grep:

var matches = jQuery.grep(array, function(item) { 
    // this is a reference to the element in the array 
    // you can do any test on it you want 
    // return true if you want it to be in the resulting matches array 
    // return false if you don't want it to be in the resulting matches array 

    // for example: to find objects with the Amount property set to a certain value 
    return(item.Amount === 100); 
}); 
// matches contains all objects that matches 
if (matches.length) { 
    // first match is in matches[0] 
} 

Se la tua condizione si vuole testare per è qualcosa di diverso da una rigorosa parità, quindi si dovrà utilizzare una sorta di matrice di iterazione che esegue il confronto personalizzato . Potresti farlo con .each() o con .grep() a seconda del tipo di output desiderato.

Se la condizione era un'uguaglianza rigorosa, è possibile utilizzare jQuery.inArray().

Ovviamente, non è necessario jQuery per questo come si potrebbe semplicemente scorrere l'array da soli in semplice javascript e implementare qualsiasi test desiderato. Un vantaggio dell'uso di javascript è che puoi uscire dall'iterazione quando hai trovato il risultato desiderato.

in JavaScript regolare:

for (var i = 0, len = array.length; i < len; i++) { 
    if (array[i].Price === 100) { 
     // match is in array[i] 
     break; 
    } 
} 
+0

Bello, questo può essere implementato in modo simile come "Elenchi generici/lambda. Dove (n => ...)" in C# – Pierre

+0

Secondo la documentazione di jQuery, '' this''' è legato all'oggetto della finestra globale, non al articolo. L'articolo è il primo parametro di quella funzione. – Andy

+0

@Andy - Thx - L'ho corretto. – jfriend00

1
$([1,2,2,4]).filter(function(i,n){return n==2;}); 

questo restituirà sia 2 di

fondamentalmente la matrice può essere un array di elementi DOM o qualsiasi array realtà, se è una matrice restituita da una selettore jQuery puoi fare qualcosa come

$('div.someClass').filter(function(){return $(this).hasClass('someOtherClass')}) 

solo per es-> questo restituirà tutti i div che ha ve sia someClass e someOtherClass (nota: ci sono altri modi per fare questo)

aggiornamento secondo il vostro commento, si può fare

$(yourArray).filter(function(i,n){return n.Amount && n.Amount == conditionValue;}); 
0

Non si ha realmente bisogno jQuery per fare quello che ti serve:

var objects = [{id:23, amount:232}, {id:42, amount: 3434}, ...] 

// the function which finds the object you want, pass in a condition function 
function findObject(objectMeetsCondition){ 
    for(var i = 0 ; i < objects.length ; i++){ 
     if(objectMeetsCondition(objects[i])) return objects[i]; 
    } 
} 

// your custom condition that determines whether your object matches 
function condition(obj){ 
    return obj.amount == 3434; 
} 

findObject(condition); // returns {id:42,amount:3434} 
Problemi correlati