4 回答
TA贡献1757条经验 获得超7个赞
找到相似的项目,然后进行相应的过滤:
const arrayOne = ['orange', 'red', 'black', 'blue', 'yellow']
const arrayTwo = ['blue', 'purple', 'white', 'red']
// [red,blue]
const dupItems = arrayOne.filter(item => arrayTwo.includes(item));
const output = [...arrayOne.filter(item => !dupItems.includes(item)), ...arrayTwo.filter(item => !dupItems.includes(item))];
console.log(output);
TA贡献1865条经验 获得超7个赞
也许这样的事情可以工作
const diffArray = arrayOne.reduce((acc, value) => {
if ((arrayTwo.inclues(value) { return acc; }
return acc.concat([value]);
}, []);
注意:请检查我的代码中是否有拼写错误,我是通过手机接听的
TA贡献1786条经验 获得超11个赞
尝试这个
function diff (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
}
// call here
diff( ['a', 'b'], ['a', 'b', 'c', 'd'] );
TA贡献1906条经验 获得超10个赞
一种解决方案是这样做:
let arrayOne=['orange', 'red', 'black', 'blue', 'yellow'];
let arrayTwo=['blue', 'purple', 'white', 'red'];
let common=arrayOne.filter(value => arrayTwo.includes(value))
let result=[...new Set([...arrayOne,...arrayTwo])].filter(value=>common.indexOf(value)==-1)
console.log(result)
添加回答
举报