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

multi splice (or similar) to remove a number of objects in array

So i currently have 2 arrays:

const getUniqueRowErrors = [1,3]

const data = [
 {identifier: '000'},
 {identifier: '111'},
 {identifier: '222'},
 {identifier: '3333'},
 {identifier: '444'}
]

The idea is i want to remove the the elements based off the getUniqueRowErrors, so I want the 1st,and 3rd element removed from the the data array, so the end result being:

const data = [
 {identifier: '111'},
 {identifier: '3333'},
 {identifier: '444'}
]

I tried the following but the desired result is incorrect:

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

const filteredData = getUniqueRowErrors.map((rowToRemove) => data.splice(rowToRemove));

Any ideas how to do the above?

>Solution :

Simply filter by index and check if the index is in the row errors array:

const getUniqueRowErrors = [1,3]

const data = [
 {identifier: '000'},
 {identifier: '111'},
 {identifier: '222'},
 {identifier: '3333'},
 {identifier: '444'}
];

console.log(data.filter((_, i) => !getUniqueRowErrors.includes(i + 1)));
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