MaxwellLynn MaxwellLynn - 6 months ago 10
Javascript Question

Remove value from array

I am trying to figure out how if you are adding values to an array and then the same value comes up you have push it to the same array to not push it up and remove the duplicate of the value from the array.

This is my code below:

if (!_.includes(scope.index, val)) {
scope.index.push(val);
} else {
_.remove(scope.index, val);
console.log(scope.index);
}

Answer

You have to call _.remove() with a predicate function which checks if the current element equals to val:

_.remove(arr, x=> x === val)
Comments