0

我正在使用 jQuery,我需要比较两个字符串数组,如下所示:

var A = ['dada','adad','a','s','rrrrrrrr'];

var B = ['dada','adad','a','s'];
C = [];

我需要比较这两个字符串数组,我需要得到差异,即比较后的'rrrrrr'值。

$.each(A, function (i, e) {
     if ($.inArray(e, B) == -1) C.push(e);
); 
alert(C);

我的输出是:

C= r,r,r,r,r,r,r,r,r,r

但是,我期望这个输出:

C=rrrrrrrrrr
4

1 回答 1

0
function exclusive(a,b){
    var c = [];

    $.each(a, function(){
        var self = this.toString();

        if(b.indexOf(self) === -1){
            c.push(self);
        }
    });

    return c;
}

例子:

var A = ['dada','adad','a','s','rrrrrrrr'];

var B = ['dada','adad','a','s'];

exclusive(A,B)

[“rrrrrrrr”]

于 2013-02-05T15:22:10.983 回答