-1

My array (this.serviceTable) includes an object, that looks like this:

[
    {
        "0": {
            "service": "Service 0"
        },
        "id": 0
    },
    {
        "1": {
            "service": "Service 1"
        },
        "id": 1
    },
    {
        "2": {
            "service": "Service 2"
        },
        "id": 2
    }
]

And from that array, I want to delete the following:

[
    {
        "2": {
            "service": "Service 2"
        },
        "id": 2
    },
    {
        "0": {
            "service": "Service 0"
        },
        "id": 0
    }
]

So the result should be:

[
    {
        "1": {
            "service": "Service 1"
        },
        "id": 1
    }
]

This is what I have tried:

const SELECTED_IDS:Array<number> = [];
for (let item of this.selection.selected) {
  SELECTED_IDS.push(item.id);
}

console.log(
  this.serviceTable.filter((serviceValue, i) => {
    !SELECTED_IDS.includes(i);
  }
), 'result');
}

But that returns an empty array.

xRay
  • 543
  • 1
  • 5
  • 29
  • Do you want to delete all items not included int the SELECTED_IDS array?? – Aymen Ben Salah May 29 '22 at 22:40
  • 1
    if that what you want just write change to this line serviceTable.filter(serviceValue=>{ return !SELECTED_IDS.includes(serviceValue.id) } – Aymen Ben Salah May 29 '22 at 22:41
  • Does this answer your question? [When should I use a return statement in ES6 arrow functions](https://stackoverflow.com/questions/28889450/when-should-i-use-a-return-statement-in-es6-arrow-functions) – pilchard May 29 '22 at 23:00

1 Answers1

3

Your arrow function syntax has problems. If you use curly brackets the function does not automatically return anything like it does without the curly brackets. And filter function expects that the function return a boolean.

See the docs.

Here is the correction:

console.log(
  this.serviceTable.filter((serviceValue, i) => !SELECTED_IDS.includes(i)
), 'result');

// OR

console.log(
  this.serviceTable.filter((serviceValue, i) => {
    return !SELECTED_IDS.includes(i);
  }
), 'result');
}

vinkomlacic
  • 1,822
  • 1
  • 9
  • 19