2013-06-27 92 views
1

我有2個數組,在這2我需要一個數組,只存在於2'nd數組輸出。我試過這個,但沒有回報;如何過濾陣列只有它不在另一個數組值

var x = ["apple","2","3", "4", "3", "x", "M"], y = ["4","5","apple","zoo"]; 

var z = y.filter(function(){ 
    return $.inArray(y,x) === -1; 
}) 

console.log(z); i am looking for ["5","zoo"] 

如何過濾這樣的..任何幫助嗎?

回答

2

嘗試

var x = ["apple","2","3", "4", "3", "x", "M"], y = ["4","5","apple","zoo"]; 

var z = $.map(y, function(val, idx){ 
    return $.inArray(val, x) === -1 ? val : undefined; 
}) 

演示:Fiddle

0
var x = ["apple","2","3", "4", "3", "x", "M"] 
, y = ["4","5","apple","zoo"] 
, result = []; 
$.each(x, function (i, v) { 
    if ($.inArray(v, y) !== -1) { 
     result.push(v); 
    } 
}); 
console.log(result); 

http://jsfiddle.net/svCys/1

相關問題