Javascript 筛选器数组返回空

Javascript 筛选器数组返回空,javascript,arrays,Javascript,Arrays,我试图用检索到的所有值填充数组(我在循环中),然后删除已存在于YearsCustomSelected数组中的值: yearsJustSelected.forEach((el:any)=>{ yearsJustSelectedAll.push(el); }); yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));

我试图用检索到的所有值填充数组(我在循环中),然后删除已存在于YearsCustomSelected数组中的值:

yearsJustSelected.forEach((el:any)=>{
    yearsJustSelectedAll.push(el);
  });


  yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
例如:
循环1-->年自定义=[1,2,3]-年自定义=[1,2,3]-年自定义筛选=[]
循环2-->年自定义=[2,3,4,5]-年自定义All=[1,2,3,4,5]-年自定义Filtered=[4,5]-->因为第一个循环中的年自定义All中已经存在[2,3]

此代码:

yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));

始终返回空数组。

您可以检查目标数组是否没有该项。然后将项目推送到目标阵列

  yearsJustSelected.forEach((el:any)=>{
    yearsJustSelectedAll.push(el);
  });
// The code above will add all elements in yearsJustSelected into yearsJustSelectedAll
// making the two arrays the same.

There are no instances where one array has elements that are not in the other array
功能选择(源、目标){
source.forEach(v=>target.includes(v)| | target.push(v));
}
var yearsCustomSelectedAll=[];
选择([1,2,3],年自定义全部);
console.log(…yearsJustSelectedAll);
选择([2,3,4,5],年份自定义全部);
console.log(…yearsJustSelectedAll);
选择([4,5],年自定义全部);

console.log(…yearsJustSelectedAll)
Your
.forEach()
yearsJustSelected
中的每个值添加到
yearsJustSelectedAll
,因此,似乎没有任何情况下您的筛选条件(
!yearsJustSelectedAll.includes(el)
)将
设置为true
…您的筛选条件是“在数组元素上循环并删除任何不是数组元素的元素”。它们都是数组的元素…这就是为什么当过滤器在它们上迭代时它们会出现的原因。很抱歉,你是对的…我需要暂停!