jquery得到两个数组的差异

时间:2013-08-26 13:16:07

标签: javascript jquery arrays

我有两个javascript数组:

var lastWeek=["mofakkarul2013innovation", "basu2013monitoring", "petrov2013applying", "truong2013impact", "truong2013reduction", "laefer2013planning", "aksoy2013relative", "dormer2013carbon", "o2008plenary", "pakrashi164review", "zhao2013identification", "carey2013investigating", "jacob2013icwim", "ferreira2013life", "ferreira2013life", "cottercan", "doyleoptimal", "conlon2013anatomy", "cottercan", "doyleoptimal", "conlon2013anatomy", "golsdorfgreen", "golsdorfgreen", "hochstrasserneeded", "dieterich2013greenhouse", "hassanzadeh2013identification", "nedovic2013planning", "parnellbayesian", "fennell2013simulating", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "gauthier2013genetic", "colman2013ventbase", "yearsley2013effect", "boots2013linking", "balter2013contrasting", "roarty2013permanent", "teresa2013beef", "andriuzzi2013drilosphere", "hochstrasserneeded", "dieterich2013greenhouse", "hochstrasserneeded", "dieterich2013greenhouse", "hochstrasserneeded", "dieterich2013greenhouse", "georgiopoulou2013basement", "benetti2013late", "georgiopoulou2013critical", "parnellbayesian", "fennell2013simulating", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "balme2013geomorphology", "courtney2013statistical", "courtney2013statistical", "tuffy2013rotate", "page2013three", "tuffy2013rotate", "page2013three", "mcdermottfrontiers", "deininger2013investigating", "comas2013impacts", "convery2013european", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "mcnamara2013experimental", "courtney2013statistical", "courtney2013high", "georgiopoulou2013basement", "benetti2013late", "georgiopoulou2013critical", "vogfjord2013extending", "jonsdottir2013coda", "ying2013time", "ferreira2013life", "rafieetwo", "banner2013unexpected", "rafiee2013numerical", "viotti2013emergence", "stefanakis2013can", "carbone2013extreme", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "murphy2013ethics", "mcnamara2013experimental", "georgiopoulou2013basement", "georgiopoulou2013basement", "cooper2013mode", "carolan2013effect", "tukovic2013finite", "carolan2013arbitrary", "kanyanta2013bio", "mohan2013effect", "walter2013detachment", "doyle2013developing", "fennell2013simulating", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "vogfjord2013extending", "jonsdottir2013coda", "ying2013time", "courtney2013statistical", "schopfer2013three", "schopfer2013failure", "aksoy2013relative", "mcdermottfrontiers", "deininger2013investigating", "comas2013impacts", "rafieetwo", "banner2013unexpected", "rafiee2013numerical", "viotti2013emergence", "stefanakis2013can", "carbone2013extreme", "qi2013investigations", "li2013investigation", "liu2013review", "liu2013digital", "li2013k", "guo2013analysis", "sabol2013extracting", "sabol2013extracting", "zhao2013unitary", "callan2013crisis", "holohan2013stress", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "georgiopoulou2013basement", "georgiopoulou2013basement", "schopfer2013three", "schopfer2013failure", "mofakkarul2013innovation", "fennell2013simulating", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "mcainsh2013measurement", "mcainsh2013measurement", "ali2013identification", "ali2013brassinosteroid", "ali2013insights", "diamond2013fusarium", "reape2013commentary", "diamond2013fusarium", "hochstrasserneeded", "dieterich2013greenhouse", "schopfer2013three", "schopfer2013failure", "schopfer2013three", "schopfer2013failure", "callan2013crisis", "holohan2013stress", "callan2013crisis", "holohan2013stress", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "schopfer2013three", "schopfer2013failure", "maischberger2013expression", "liu2013hplc", "miranda2013real", "zhao2013preliminary", "zhao2013preliminary", "zhao2013preliminary", "zhao2013preliminary", "boots2013linking"]

var thisWeek=["basu2013monitoring", "gonzalez2013impact", "petrov2013applying", "truong2013impact", "truong2013reduction", "laefer2013planning", "truong2013combining", "aksoy2013relative", "dormer2013carbon", "o2008plenary", "pakrashi164review", "zhao2013identification", "carey2013investigating", "jacob2013icwim", "ferreira2013life", "convery2013european", "convery2013reflections", "kroonsixth", "wan2013bootstrapping", "o2013smart", "hynes2013towards", "o2013pervasive", "sandygulova2013study", "carr2013sixth", "dragone2013programming", "cottercan", "doyleoptimal", "conlon2013anatomy", "avino2013sovereign", "scott2013design", "igoe2013investigation", "gavin2013effect", "gavin2013base", "doherty2013pile", "golsdorfgreen", "murphy2013mortgage", "scott2013design", "hochstrasserneeded", "dieterich2013greenhouse", "hassanzadeh2013identification", "nedovic2013planning", "hassanzadeh2013identification", "nedovic2013planning", "fennell2013simulating", "prescher2013short", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "scottisolation", "dool2013phylogeography", "kirwan2013phylomedicine", "teeling2013making", "springer2013technical", "hughes2013using", "lundy2013prey", "gauthier2013genetic", "colman2013ventbase", "yearsley2013effect", "boots2013linking", "balter2013contrasting", "roarty2013permanent", "teresa2013beef", "andriuzzi2013drilosphere", "von2013skull", "hochstrasserneeded", "dieterich2013greenhouse", "kochmannenvironmental", "salvaterraimpacts", "green2013physical", "green2013effects", "crowe2013large", "gollety2013contribution", "bolger2013hierarchical", "andriuzzi2013drilosphere", "connolly2013improved", "georgiopoulou2013basement", "benetti2013late", "georgiopoulou2013critical", "parnellbayesian", "fennell2013simulating", "prescher2013short", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "balme2013geomorphology", "ramsdale2013sinuous", "courtney2013statistical", "courtney2013high", "scott2013design", "tuffy2013rotate", "page2013three", "brooks2013production", "mcdermottfrontiers", "deininger2013investigating", "comas2013impacts", "comas2013impacts", "warren2013archaeological", "convery2013european", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "mcnamara2013experimental", "courtney2013statistical", "courtney2013high", "georgiopoulou2013basement", "benetti2013late", "georgiopoulou2013critical", "vogfjord2013extending", "jonsdottir2013coda", "ying2013time", "ferreira2013life", "ferreira2013life", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "murphy2013ethics", "mcnamara2013experimental", "georgiopoulou2013basement", "georgiopoulou2013basement", "cooper2013mode", "carolan2013effect", "tukovic2013finite", "carolan2013arbitrary", "kanyanta2013bio", "mohan2013effect", "walter2013detachment", "doyle2013developing", "fennell2013simulating", "prescher2013short", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "vogfjord2013extending", "jonsdottir2013coda", "ying2013time", "doyle2013developing", "courtney2013statistical", "courtney2013high", "schopfer2013three", "schopfer2013failure", "schopfer2013three", "schopfer2013failure", "mcdermottfrontiers", "deininger2013investigating", "comas2013impacts", "comas2013impacts", "rafieetwo", "banner2013unexpected", "rafiee2013numerical", "viotti2013emergence", "stefanakis2013can", "carbone2013extreme", "dutykh2013use", "qi2013investigations", "li2013investigation", "liu2013review", "liu2013digital", "li2013k", "guo2013analysis", "sabol2013extracting", "sabol2013extracting", "zhao2013unitary", "ryle2013calibration", "callan2013crisis", "holohan2013stress", "igoe2013investigation", "gavin2013effect", "gavin2013base", "doherty2013pile", "lynch2013utilisation", "everard2013evaluation", "o2013feasibility", "devlin2013fuel", "wolff2013practical", "sweeney2013development", "mcdonnell2013photo", "omara2013anaerobic", "mcdonnell2013fabrication", "goodbody2013regional", "narancic2013highly", "molloy2013engineering", "babu2013current", "casey2013effect", "brooks2013production", "o2013anti", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "georgiopoulou2013basement", "georgiopoulou2013basement", "schopfer2013three", "schopfer2013failure", "mofakkarul2013innovation", "fennell2013simulating", "prescher2013short", "saunders2013land", "nichonchubhair2013greenhouse", "saunders2013impact", "gioria2013biological", "mcainsh2013measurement", "brooks2013production", "ali2013identification", "ali2013brassinosteroid", "ali2013insights", "diamond2013fusarium", "mullins2013method", "reape2013commentary", "diamond2013fusarium", "hochstrasserneeded", "dieterich2013greenhouse", "schopfer2013three", "schopfer2013failure", "kavanagh2013microbial", "amadio2013filamentous", "hamza2013isolation", "robertson2013synthesis", "callan2013crisis", "holohan2013stress", "feeley2013regional", "o2013performance", "nasr2013derivation", "feeley2013regional", "ahilan2013bankfull", "neil2013modelling", "o2013parameter", "kochmannenvironmental", "salvaterraimpacts", "green2013physical", "green2013effects", "crowe2013large", "gollety2013contribution", "schopfer2013three", "schopfer2013failure", "maischberger2013expression", "liu2013hplc", "miranda2013real", "zhao2013preliminary", "kavanagh2013microbial", "amadio2013filamentous", "hamza2013isolation", "robertson2013synthesis", "brooks2013production", "narancic2013highly", "molloy2013engineering", "babu2013current", "casey2013effect", "brooks2013production", "o2013anti", "boots2013linking"]
“lastWeek”是上周的论文清单。 'thisWeek'是本周的论文清单。

如何在jQuery中获取新论文? (即排除上周出现的所有论文)

我试过了:

var diff=$(thisWeek).not(lastWeek).get()

但这给了我错误的差异 - 差异中的论文都没有出现在本周的名单中:他们都在上周的差异中。 ;(

非常感谢,

4 个答案:

答案 0 :(得分:1)

来自Source

jQuery.grep(array1, function(el)
{
    return jQuery.inArray(el, array2) == -1;
});

答案 1 :(得分:1)

var diff = [];
var i = 0;
jQuery.grep(lastWeek, function(el) {
    if (jQuery.inArray(el, thisWeek) == -1) diff.push(el);
    i++;
});

答案 2 :(得分:1)

它应该是

var diff=$(lastWeek).not(thisWeek).get();

你做得对,但为什么你在jQuery中使用不同的变量?

编辑:如果上面没有工作(我认为是正确的btw)然后创建一个新数组,

var _diff = new Array();

并且这样做:

_diff = jQuery.grep(lastWeek, function (whatsNew) {
     return jQuery.inArray(whatsNew, thisWeek) < 0;
});

答案 3 :(得分:1)

var difference = [];
for(paper in thisWeek){    
    if($.inArray(thisWeek[paper], lastWeek) == -1){
    difference.push(thisWeek[paper]);    
    }
}