There are a lot of ways to make this more modern code, each step I show here is a based upon the previous:
#1 Use for...of
loops instead of manually using an index variable (also use const
since this never changes during the same iteration)
const filteredData = csiList.filter((item) => {
for (const data of this.tableData) {
if (data.csiId == item.appId) {
return false;
}
}
return true;
});
#2 Use Destructuring to just get the necessary data (this is an opinion of if it's better or not, but I'm using more modern features since that's what you have asked for)
const filteredData = csiList.filter(({ appId }) => {
for (const { csiId } of this.tableData) {
if (csiId == appId) {
return false;
}
}
return true;
});
#3 Use Array.prototype.some
, built for finding if at least one item in a list meets a criteria. Inverted here with !
, since it returns true
if at least one items meets the criteria. (Alternatively, use every
with !==
)
const filteredData = csiList.filter(({ appId }) => {
return !this.tableData.some(({ csiId }) => csiId === appId);
});
#4 Use the implicit return of an arrow function with {
and }
(on a new line because the width of each line is limited to 80 characters by Prettier's opinionated standard)
const filteredData = csiList.filter(
({ appId }) => !this.tableData.some(({ csiId }) => csiId === appId),
);