假设我有个数组,[34, 35, 45, 48, 49]另一个数组[48, 55]。我如何得到结果数组[34, 35, 45, 48, 49, 55]?
[34, 35, 45, 48, 49]
[48, 55]
[34, 35, 45, 48, 49, 55]
如果您不需要保留订单,并考虑45和"45"保持一致:
45
"45"
function union_arrays (x, y) { var obj = {}; for (var i = x.length-1; i >= 0; -- i) obj[x[i]] = x[i]; for (var i = y.length-1; i >= 0; -- i) obj[y[i]] = y[i]; var res = [] for (var k in obj) { if (obj.hasOwnProperty(k)) // <-- optional res.push(obj[k]); } return res; } console.log(union_arrays([34,35,45,48,49], [44,55]));