gpt4 book ai didi

javascript - 如何删除两个数组上的相同值?

转载 作者:行者123 更新时间:2023-12-03 00:12:10 25 4
gpt4 key购买 nike

我想从两个表中删除相同的值,例如:

var array1 = ["1", "2", "3", "4", "5"]
var array2 = ["3", "4", "5"]

我想要结果:

var result = ["1", "2"]



最佳答案

您可以使用filter()includes()concat()。它适用于两个数组

var array1 = ["1", "2", "3", "4", "5"]
var array2 = ["3", "4", "5","6"]

let result = array1.filter(x => !array2.includes(x)).concat(array2.filter(x => !array1.includes(x)))
console.log(result);

关于javascript - 如何删除两个数组上的相同值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com