简体   繁体   English

试图比较两个对象的 arrays 并从一个数组中删除匹配的元素并返回它,目前只删除第一个匹配项

[英]Trying to compare two arrays of objects and remove matching elements from one array and return it, currently only removing first match

I have a filterSection object array and a customFilter array and I'm trying to loop through both and remove any matching elements from the filterSection array.我有一个 filterSection object 数组和一个 customFilter 数组,我试图遍历这两个数组并从 filterSection 数组中删除任何匹配的元素。

The problem I'm running into is that I can only seem to remove the first match and nothing beyond that and I'm not sure what I'm doing wrong.我遇到的问题是我似乎只能删除第一场比赛,除此之外什么都没有,我不确定我做错了什么。

This is my function for compare/remove:这是我的 function 用于比较/删除:

const newFilterList = (filterSelection) => {
    for( var i = 0; i < filterSelection.length; i++){
      for( var j = 0; j < customFilter.length; j++) 
        if ( filterSelection[i].section === customFilter[j].section) { 
          return filterSelection = filterSelection.splice(i, 1); 
        }
  }
}

And these are my two object arrays:这些是我的两个 object arrays:

customFilter

[
 {
  section: 'Artists',
  filters: ['LVNDMARKS']
 },
 {
   section: 'Years',
   filters: ['2021']
 }
]

And my filterSelection is this:我的filterSelection是这样的:

[
 {
   section: 'Artists',
   filters: ['LVNDMARKS', 'Born of Osiris', 'Intervals']
 },
 {
   section: 'Years',
   filters: ['2021','2019','2017']
 },
 {
  section: 'Albums',
  filters: ['albumName', 'albumName', 'albumName',]
 }
]

Try this:尝试这个:

 const data = [ { sec: "Artists", fil: ["LVNDMARKS", "Born of Osiris", "Intervals"] }, { sec: "Years", fil: ["2021", "2019", "2017"] }, { sec: "Albums", fil: ["albumName", "albumName", "albumName"] } ]; const conditions = [ { sec: "Artists", fil: ["LVNDMARKS"] }, { sec: "Years", fil: ["2021"] } ]; conditions.forEach(cond => { data.filter(el => el.sec == cond.sec).forEach(match => { match.fil = match.fil.filter(filEl => cond.fil.indexOf(filEl) == -1); console.log(match.fil); }); });

I've renamed your objects as it was quite confusing mixing "filter" the property and "filter" the array operator.我已经重命名了您的对象,因为混合“过滤”属性和“过滤”数组运算符非常令人困惑。

Adding codepen for convenience.为方便起见,添加codepen

Two problems, you are returning on the first match, and you are mutating the original filterSelection array in the loop so the length changes and you don't iterate over the entire array.两个问题,您将在第一个匹配项上返回,并且您正在循环中改变原始filterSelection数组,因此长度会发生变化,并且您不会遍历整个数组。 You should store it to a temporary array to avoid this problem.您应该将其存储到临时数组中以避免此问题。 Something like this:像这样的东西:

const newFilterList = (filterSelection) => {
  let newFilterSelection = []
  for (var i = 0; i < filterSelection.length; i++) {
    for (var j = 0; j < customFilter.length; j++) {
      if (filterSelection[i].section === customFilter[j].section) {
        newFilterSelection = filterSelection.splice(i, 1);
      }
    }
  }
  filterSelection = newFilterSelection
};

The expression i < filterSelection.length is evaluated at the beginning of each loop:表达式i < filterSelection.length在每个循环开始时计算:

 for ([initialization]; [condition]; [final-expression]) statement

condition (健康)状况

An expression to be evaluated before each loop iteration.每次循环迭代之前要评估的表达式。 If this >expression evaluates to true, statement is executed.如果此 > 表达式的计算结果为真,则执行语句。 This conditional test is optional.此条件测试是可选的。 If omitted, the condition always evaluates to true.如果省略,则条件始终计算为真。 If the expression evaluates to false, execution skips to the first expression following the for construct.如果表达式的计算结果为 false,则执行跳到 for 构造之后的第一个表达式。

MDN Web Docs - For Loops MDN Web 文档 - For 循环

don't use a return statement because it terminates your loop when the if condition true for the first time.不要使用 return 语句,因为它会在 if 条件第一次为真时终止循环。 so you can easily push matching data in an array & return it因此您可以轻松地将匹配的数据推送到数组中并返回

 const customFilter = [ { section: "Artists", filters: ["LVNDMARKS"] }, { section: "Years", filters: ["2021"] } ]; const filterSelection = [ { section: "Artists", filters: ["LVNDMARKS", "Born of Osiris", "Intervals"] }, { section: "Years", filters: ["2021", "2019", "2017"] }, { section: "Albums", filters: ["albumName", "albumName", "albumName"] } ]; const newFilterList = filterSelection => { let empty = []; for (var i = 0; i < filterSelection.length; i++) { for (var j = 0; j < customFilter.length; j++) if (filterSelection[i].section === customFilter[j].section) { empty.push(filterSelection.splice(i, 1)); } } return empty; }; console.log( newFilterList(filterSelection))

or

 const customFilter = [ { section: "Artists", filters: ["LVNDMARKS"] }, { section: "Years", filters: ["2021"] } ]; const filterSelection = [ { section: "Artists", filters: ["LVNDMARKS", "Born of Osiris", "Intervals"] }, { section: "Years", filters: ["2021", "2019", "2017"] }, { section: "Albums", filters: ["albumName", "albumName", "albumName"] } ]; const newFilterList = filterSelection => { let empty = []; filterSelection.forEach(({section},i)=>{ customFilter.map(res=>{ if(section===res.section){ empty.push(filterSelection[i]) } }) }) return empty; }; console.log(newFilterList(filterSelection))

let customFilter = [
 {
  section: 'Artists',
  filters: ['LVNDMARKS']
 },
 {
   section: 'Years',
   filters: ['2021']
 }
];

let filterSelection = [
 {
   section: 'Artists',
   filters: ['LVNDMARKS', 'Born of Osiris', 'Intervals']
 },
 {
   section: 'Years',
   filters: ['2021','2019','2017']
 },
 {
  section: 'Albums',
  filters: ['albumName', 'albumName', 'albumName',]
 }
];

let newFilter = [];

filterSelection.forEach(i => {  
  if(!customFilter.some(ii => ii.section === i.section))
    newFilter.push(i);
});

console.log(newFilter);

Here is a different approach.这是一种不同的方法。 To store the indexes and splice them afterward.存储索引并在之后拼接它们。

const newFilterList = (filterSelection) => { 
    let indexToRemove = [];
    let customFilterArray = [];
    for(let temp of customFilter)
        customFilterArray.push(temp.section);
    filterSelection.forEach((el,index)=>{
        if(customFilterArray.includes(el.section))
            indexToRemove.push(index)
    })
  while(indexToRemove.length>0) 
    {
      filterSelection.splice(indexToRemove[indexToRemove.length - 1],1);
      indexToRemove.pop();
    }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个具有对象的 arrays 并从第一个数组中删除重复项 - Compare two arrays having objects and remove duplicates from first array 通过Id比较两个数组的元素,并从一个数组中删除未在另一个数组中显示的元素 - Compare the elements of two arrays by Id and remove the elements from the one array that are not presented in the other 对于两个或更多 arrays 对象,如何删除第一个元素,直到所有第一个元素在字段上匹配 - For two or more arrays of objects how can you remove first elements until all first elements match on a field 比较两个对象,仅返回第一个对象的匹配值 - Compare two objects and return only the matched values from the first object 如何比较两个对象数组,如果第二个数组项值如此说明,则仅从第一个数组中删除项 - How to compare two arrays of objects and only remove items from 1st array if 2nd arrays item value says so 比较两个对象数组,如果匹配则返回 true - Compare two array of objects and return true if they are match 比较两个 arrays 的对象并根据一个对象数组排序并在不匹配时推送 - compare two arrays of objects and sort based on one array of objects and push when doesn't match 比较对象的 arrays 并返回不在 arrays 之一中的新对象数组 - Compare arrays of objects and return new array of objects that are not in one of the arrays 比较两个数组中的对象并根据javascript中的匹配返回 - Compare objects in two arrays and return based on match in javascript 比较两个 arrays 并返回一个新数组,其中包含仅在原始 arrays 之一中找到的任何项目 - Compare two arrays and return a new array with any items only found in one of the original arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM