Typescript issue - .filter item used with foreach is ignored/not read in

Hello,

For the script below:

  1. LINE 7: I’m filtering only to item.isRemoved = false

  2. doing a foreach loop on these filtered items

  3. I expect live preview of the function to only show items === false before changing to true, but there are a few issues:

  • line 7 is grayed out (‘activeExclusions’ is declared but its value is never read.typescript(6133)
  • since it’s not being/acting like it is read in, it still shows items === true

I cannot see any errors/issues. Do you see anything I am missing? Thank you!!

@Edits(XXXExclusions)
@OntologyEditFunction()
public async YYYDelete(
XXXExclusions: XXXExclusions
): Promise {
//mark exclusions as false to take of list, will filter from workshop app
const activeExclusions = xxxExclusions.filter(item => item.isRemoved === false);
xxxExclusions.forEach(activeExclusions => {
activeExclusions.isRemoved = true;
});
}

You are not returning anything, nor are you editing the Ontology, so everything is working as expected.

The thing that would be relevant to know is what you are actually trying to do. I assume you’re trying to set some objects to be excluded?

If you’re doing this from workshop, it is pretty easy to e.g. create two object tables, each using a filtered ObjectSet (one with isRemoved === false and one where isRemoved === true) and you could then have a button that would flip the state, e.g. move one from the ‘true’-table to the ‘false’-table.

If you’re trying to show whether an object is set as removed or not, you could create an object table with the full ObjectSet and then use value formatting on the property, to mark true as red and false as green (removed / not removed).

To look at your code, the linter is right – you’re not using this variable:

activeExclusions

Your code does the following:

  1. Creates an array of XXXExclusions-objects (unused)
  2. Loop through all XXXExclusions-objects (not just the active ones)
  3. Set every single item to isRemoved = true (not just the active ones)
  4. Returns nothing
  5. You also need to await calls when you’re writing an async function

Passing activeExclusions as a parameter when running the for-loop, actually results in that name being shadowed, meaning you will be iterating over all items in xxxExclusions and setting each one to isRemoved = true.

To fix your code, try something like this:

@Edits(XXXExclusions)
@OntologyEditFunction()
public async YYYDelete(exclusions: XXXExclusions[])
: Promise<void> {
    const exclusionPromises = exclusions.map(async (exclusion) => {
        if(!exclusion.isRemoved) {
            exclusion.isRemoved = true;
            }
        });
    await Promise.all(exclusionPromises);
}