How to delete two array elements at the same time

Let's assume that we are working with the following array:

javascript
let array = ['Erik', 'Andrea', 'Paula', 'Erik'];

Let's assume that we want to remove positions 0 and 3 at the same time:

javascript
let remove = [0, 3];

Let's see how to do it step by step:

javascript
// ['Erik', 'Andrea', 'Paula', 'Erik'] => [null, 'Andrea', 'Paula', null]
let updated = array.map((name, i) => remove.indexOf(i) > -1 ? null : name);

// Filtering 'null' values
let res = updated.filter(names => names !== null);

console.log(res);
// ['Andrea', 'Paula']

In one line of code:

javascript
let res = array.map((name, i) => remove.indexOf(i) > -1 ? null : name).filter(names => names !== null);

console.log(res);
// ['Andrea', 'Paula']

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.