2014-10-19 4 views
0

JQuery에서 변수를 2 차원 배열로 선언했습니다.JQuery에서 2 차원 배열 검색

length: 4 
[0]: {...} 
[1]: {...} 
[2]: {...} 
[3]: {...} 

4 개 각 요소는, 예를 들어 같은 고유 키와 값을 포함합니다 : 내 예에서 배열의 첫 번째 차원은 4 개 요소를 가지고 내가하고 싶은 무엇

Key: "Some key" 
Value: "This is some value" 

배열을 검색하고 키가 "Some key"와 같은 값을 가져옵니다. JQuery로 한 줄 또는 두 줄로 우아하게 처리 할 수 ​​있습니까?

+0

주 그 요소는 같은 키가있는 경우' Key'와'Value'는 배열이 아닌 * objects *처럼 들리므로 객체 배열은 2 차원 배열이 아닙니다. (자바 스크립트는 실제로 어차피 2 차원 배열을 가지고 있지 않지만 ...) –

답변

2

물론 :

$.each(theArray, function(index, entry) { 
    // Use entry.Key and/or entry.Value here 
}); 

또는 현대의 브라우저에서 jQuery를하지 않고

:

theArray.forEach(function(entry) { 
    // Use entry.Key and/or entry.Value here 
}); 

(forEach는 IE8에 shimmed과 같은 수 있습니다.)

당신이에 중지하려면 먼저 일치 :

$.each(theArray, function(index, entry) { 
    if (/* Use entry.Key and/or entry.Value here*/) { 
     return false; // Ends the "loop" 
    } 
}); 

또는 현대의 브라우저에서 jQuery를하지 않고 : (. someevery IE8에 shimmed 등 가능)

theArray.some(function(entry) { 
    if (/* Use entry.Key and/or entry.Value here*/) { 
     return true; // Ends the "loop" 
    } 
}); 

또는

theArray.every(function(entry) { 
    if (/* Use entry.Key and/or entry.Value here*/) { 
     return false; // Ends the "loop" 
    } 
});