2012-03-17 121 views
0

所以,標題說大部分,我會舉一個例子:比較字符串元素反對另一個數組的元素屬性

var foo = ['one', 'two', 'three']; 
var bar = [ 
     { 
      integer: 1, 
      string: 'one' 
     }, 
     { 
      integer: 3, 
      string: 'three' 
     }  
    ]; 

現在,我不知道我怎麼能得到foo中每個元素的正匹配列表與陣列bar中所有對象的每個string屬性相對應。

回答

4

首先,一組foo的所有元素,以避免多複雜:

var fooSet = {};  
for(var i = 0; i < foo.length; i++) { 
    var e = foo[i]; 
    fooSet[e] = true; 
} 

然後,經過bar並收集比賽:

var matches = []; 
for(var i = 0; i < bar.length; i++) { 
    var e = bar[i]; 
    if(fooSet[e.string]) { 
      matches.push(e); 
    } 
} 

在此之後,matches陣列將包含來自bar的元素匹配foo的元素。

這裏是一個活生生的例子:

+0

+1。創建'fooSet'使得這比我使用'.indexOf()'的回答更好(特別是現在你已經刪除了for..in循環)。 – nnnnnn 2012-03-17 02:35:32

+0

確實如此,但是這隻在foo和bar長度相等時纔有效,這不在我的用例中。這就是爲什麼我給了這樣一個例子。 http://jsfiddle.net/mfAc4/ – Milos 2012-03-17 10:58:15

+1

它適用於不同長度的'foo'和'bar' - 下面是一個例子,其中'foo'有8個,'bar'有3個元素:http://jsfiddle.net/mfAc4/1 /,下面是一個例子,其中'foo'有2個,'bar'有3個元素:http://jsfiddle.net/mfAc4/2/。這是你想要的? – 2012-03-17 16:21:13

0

循環遍歷數組檢查,對其他的每個項目之一:

var matches = [], 
    i, j; 

for (i=0; i < bar.length; i++) 
    if (-1 != (j = foo.indexOf(bar[i].string))) 
     matches.push(foo[j]); 

// matches is ['one', 'three'] 

顯然,如果你想matches遏制來自bar的項目而不是foo只需更改.push() s處理至matches.push(bar[i]);

當然,上述假設你並不在乎支持在陣列上不支持.indexOf()的舊瀏覽器,或者你可以使用a shim

0

此:

// foo & bar defined here 

// put all the keys from the foo array to match on in a dictionary (object) 
var matchSet = {}; 
for (var item in foo) 
{ 
    var val = foo[item]; 
    matchSet[val] = 1; 
} 

// loop through the items in the bar array and see if the string prop is in the matchSet 
// if so, add it to the matches array 
var matches = []; 
for (var i=0; i < bar.length; i++) 
{ 
    var item = bar[i]; 
    if (matchSet.hasOwnProperty(item['string'])) 
    { 
     matches.push(item['string']); 
    } 
} 

// output the matches (using node to output to console) 
for (var i in matches) 
{ 
    console.log(matches[i]); 
} 

輸出:

match: one 
match: three 

注意:在使用節點來運行交互

0

這2工作。取決於你想如何使用你可以修改的數據。

http://jsfiddle.net/KxgQW/5/

途徑之一

//wasn't sure which one you want to utilize so I'm doing both 
//storing the index of foo to refer to the array 
var wayOneArrayFOO= Array(); 
//storing the index of bar to refer to the array 
var wayOneArrayBAR= Array(); 

var fooLength = foo.length; 
var barLength = bar.length; 


/*THE MAGIC*/     
//loop through foo 
for(i=0;i<fooLength;i++){ 

    //while your looping through foo loop through bar   
    for(j=0;j<barLength;j++){ 
     if(foo[i]==bar[j].string){ 
      wayOneArrayFOO.push(i); 
      wayOneArrayBAR.push(j);  
     } 
    } 

} 

路兩

//storing the value of foo 
var wayTwoArrayFOO= Array(); 
//storing the value of bar 
var wayTwoArrayBAR= Array();    


/*THE MAGIC*/  
//loop through foo 
for(i=0;i<fooLength;i++){ 

    //while your looping through foo loop through bar   
    for(j=0;j<barLength;j++){ 
     if(foo[i]==bar[j].string){ 
      wayTwoArrayFOO.push(foo[i]); 
      wayTwoArrayBAR.push(bar[j]);  
     } 
    } 

}