Q:

Write a JavaScript program to mutate the original array to filter out the values specified. Returns the removed elements

0

Write a JavaScript program to mutate the original array to filter out the values specified. Returns the removed elements

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const pullAtValue = (arr, pullArr) => {
  let removed = [],
    pushToRemove = arr.forEach((v, i) => (pullArr.includes(v) ? removed.push(v) : v)),
    mutateTo = arr.filter((v, i) => !pullArr.includes(v));
  arr.length = 0;
  mutateTo.forEach(v => arr.push(v));
  return removed;
};
let myArray = ['a', 'b', 'c', 'd'];
let pulled = pullAtValue(myArray, ['b', 'd']); 
console.log('Original data',myArray);
console.log('Pulled data',pulled);

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now