Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

remove elements from array that has same value from other array

// array which holds all values
const namesArr = ["Lily", "Roy", "John", "Jessica"];
const namesToDeleteArr = ['Roy', 'John']
const namesToDeleteSet = new Set(namesToDeleteArr);

const newArr = namesArr.filter((name) => {
  return !namesToDeleteSet.has(name);
});

console.log(newArr); // ["Lily", "Jessica"]

//////////// OR //////////////////////////

function arr(integerList, valuesList){
  return integerList.filter(element => {
    return valuesList.indexOf(element) === -1
  })
}

console.log(arr([1, 1, 2 ,3 ,1 ,2 ,3 ,4], [1, 3])) // [ 2, 2, 4 ]

//////////// OR /////////////////////////

function arr(a, valuesList){
  const newArr = []
  for (let i = 0; i < a.length; i++){
    if (!valuesList.includes(a[i]))
      newArr.push(a[i])
  }
  return newArr
}

console.log(arr([1, 1, 2 ,3 ,1 ,2 ,3 ,4], [1, 3]))
Source by melvingeorge.me #
 
PREVIOUS NEXT
Tagged: #remove #elements #array #array
ADD COMMENT
Topic
Name
6+3 =