JS陣列取相同的值和不同的值
阿新 • • 發佈:2018-10-31
var arr1 = ["1", "2", "3"]; var arr2 = [1, "f", "g", 3]; var arr3 = []; for (var s in arr1) { for (var x in arr2) { if (arr1[s] == arr2[x]) { arr3.push(arr1[s]); } } } alert("相同的是:"+arr3); var a = [1, 2, 3, 4, 5, 6, 7]; var b = [1, 2, 5]; var c = []; var tmp = a.concat(b); var o = {}; for (var s = 0; s < tmp.length; s++) { (tmp[s] in o) ? o[tmp[s]]++ : o[tmp[s]] = 1; } for (x in o) if (o[x] == 1) c.push(x); alert("不相同的是:"+c);