[英]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.我已经重命名了您的对象,因为混合“过滤”属性和“过滤”数组运算符非常令人困惑。
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 构造之后的第一个表达式。
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.