Search code examples
javascriptangularjslodash

How can I remove an item from an array using either javascript or lodash


My object is below. I am using this in angular and have lodash injected into it. I want to remove the 2nd item using choice2 as what I would pass to match.

    $scope.choices = [{ id: 'choice1' }, { id: 'choice2' }, { id: 'choice3' 

Solution

  • You can use the remove method of lodash.

    $scope.choices = [{ id: 'choice1' }, { id: 'choice2' }, { id: 'choice3' }];
    _.remove($scope.choices, function(n) {
        return n.id == 'choice2';
    });