猿问

数组对比差异

var arr1 = [

    "574417bc79df540065d92df7",

    "574424e5df0eea0063adefc6",

    "57442329a3413100625f194f",

    "5744242bc4c971005d5ff04e",

    "574a841d1532bc006068c6c9",

    "574a97fe2b51e90056e423c0"

];

var arr2 = [

    "574417bc79df540065d92df7",

    "57442329a3413100625f194f",

    "5744242bc4c971005d5ff04e",

    "574424e5df0eea0063adefc6",

    "574a841d1532bc006068c6c9"

];

如何快速地对比出这两个数组中不是同时存在的元素。
目测答案是:"574a97fe2b51e90056e423c0"

慕码人2483693
浏览 817回答 1
1回答

aluckdog

比较初级的哈哈,var&nbsp;lessArr,maxArr;if(arr1.length>arr2.length)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;lessArr&nbsp;=&nbsp;arr2;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;maxArr&nbsp;=&nbsp;arr1;&nbsp; }else{ &nbsp;&nbsp;&nbsp;&nbsp;lessArr&nbsp;=&nbsp;arr1;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;maxArr&nbsp;=&nbsp;arr2;&nbsp; }var&nbsp;lessStr&nbsp;=&nbsp;lessArr.join('|');var&nbsp;diffArr&nbsp;=&nbsp;[];for(var&nbsp;i=0;i<maxArr.length;i++)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;if(lessStr.indexOf(maxArr[i])&nbsp;==&nbsp;-1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diffArr.push(maxArr[i]); &nbsp;&nbsp;&nbsp;&nbsp;} }console.log(diffArr.join(','));
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答