1

我有 2 个数组,在这 2 个中我需要一个数组的输出,它只存在于第 2 个数组中。我试过了,但没有重复;

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"]

我如何过滤这样的..有什么帮助吗?

4

2 回答 2

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;
})

演示:小提琴

于 2013-06-27T12:17:02.033 回答
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

于 2013-06-27T12:16:15.810 回答