Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting an empty array when removing objects from array

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "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.

>Solution :

Your arrow function syntax is wrong. 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');
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading