Let's assume that we are working with the following array:
javascriptlet array = ['Erik', 'Andrea', 'Paula', 'Erik'];
Let's assume that we want to remove positions 0 and 3 at the same time:
javascriptlet 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:
javascriptlet 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.