Javascript 如何删除重复项并计算两个数组之间的差异?
我可以在嵌套映射中获得相同值的值。但是如何区分两者呢 我尝试了map,filter,但是我不知道如何正确地删除重复的值Javascript 如何删除重复项并计算两个数组之间的差异?,javascript,ecmascript-6,Javascript,Ecmascript 6,我可以在嵌套映射中获得相同值的值。但是如何区分两者呢 我尝试了map,filter,但是我不知道如何正确地删除重复的值 const responseData = [ { value: "red", label: "Red" }, { value: "blue", label: "Blue" } ]; const fixedColors = [ { value: "red", label: "Red" }, { value: "blue", label: "Blue" }, {
const responseData = [
{ value: "red", label: "Red" },
{ value: "blue", label: "Blue" }
];
const fixedColors = [
{ value: "red", label: "Red" },
{ value: "blue", label: "Blue" },
{ value: "yellow", label: "Yellow" },
{ value: "orange", label: "Orange" }
];
responseData.map(opt => {
fixedColors.findIndex(obj => {
if (obj.value === opt.value) {
testArray.push(opt);
} else {
testArray2.push(obj);
}
});
});
我可以在两个数组上得到相同的值,但不能得到差异。我不知道如何使用ES6正确执行它。
过滤器
您的响应只包括fixedColors
中未包含的值,以及concat
结果与fixedColors
const responseData=[{value:“紫色”,标签:“紫色是新的”},{value:“蓝色”,标签:“蓝色”}]
const fixedColors=[{value:“red”,label:“red”},{value:“blue”,label:“blue”},{value:“yellow”,label:“yellow”},{value:“orange”,label:“orange”}]
const existingValues=fixedColors.map(x=>x.value)
const valuesToAdd=responseData.filter(x=>!existingValues.includes(x.value))
const newValues=fixedColors.concat(valuesToAdd)
console.log(newValues)