Remove an item from array using UnderscoreJS

后端 未结 11 2117
一向
一向 2020-12-04 06:02

Say I have this code

var arr = [{id:1,name:\'a\'},{id:2,name:\'b\'},{id:3,name:\'c\'}];

and I want to remove the item with id = 3 from the array. Is th

相关标签:
11条回答
  • 2020-12-04 06:14

    You can use Underscore .filter

        var arr = [{
          id: 1,
          name: 'a'
        }, {
          id: 2,
          name: 'b'
        }, {
          id: 3,
          name: 'c'
        }];
    
        var filtered = _(arr).filter(function(item) {
             return item.id !== 3
        });
    

    Can also be written as:

    var filtered = arr.filter(function(item) {
        return item.id !== 3
    });
    
    var filtered = _.filter(arr, function(item) {
        return item.id !== 3
    });
    

    Check Fiddle

    You can also use .reject

    0 讨论(0)
  • 2020-12-04 06:15

    Use can use plain JavaScript's Array#filter method like this:

    var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
    
    var filteredArr = arr.filter(obj => obj.id != 3);
    
    console.log(filteredArr);

    Or, use Array#reduce and Array#concat methods like this:

    var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
    
    var reducedArr = arr.reduce((accumulator, currObj) => {
      return (currObj.id != 3) ? accumulator.concat(currObj) : accumulator;
    }, []);
    
    console.log(reducedArr);

    NOTE:

    • Both of these are pure functional approach (i.e. they don't modify the existing array).
    • No, external library is required in these approach (Vanilla JavaScript is enough).
    0 讨论(0)
  • 2020-12-04 06:15

    I used to try this method

    _.filter(data, function(d) { return d.name != 'a' });
    

    There might be better methods too like the above solutions provided by users

    0 讨论(0)
  • 2020-12-04 06:18

    Please exercise care if you are filtering strings and looking for case insensitive filters. _.without() is case sensitive. You can also use _.reject() as shown below.

    var arr = ["test","test1","test2"];
    
    var filtered = _.filter(arr, function(arrItem) {
        return arrItem.toLowerCase() !== "TEST".toLowerCase();
    });
    console.log(filtered);
    // ["test1", "test2"]
    
    var filtered1 = _.without(arr,"TEST");
    console.log(filtered1);
    // ["test", "test1", "test2"]
    
    var filtered2 = _.reject(arr, function(arrItem){ 
        return arrItem.toLowerCase() === "TEST".toLowerCase();
    });
    console.log(filtered2);
    // ["test1", "test2"]
    
    0 讨论(0)
  • Just using plain JavaScript, this has been answered already: remove objects from array by object property.

    Using underscore.js, you could combine .findWhere with .without:

    var arr = [{
      id: 1,
      name: 'a'
    }, {
      id: 2,
      name: 'b'
    }, {
      id: 3,
      name: 'c'
    }];
    
    //substract third
    arr = _.without(arr, _.findWhere(arr, {
      id: 3
    }));
    console.log(arr);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

    Although, since you are creating a new array in this case anyway, you could simply use _.filter or the native Array.prototype.filter function (just like shown in the other question). Then you would only iterate over array once instead of potentially twice like here.

    If you want to modify the array in-place, you have to use .splice. This is also shown in the other question and undescore doesn't seem to provide any useful function for that.

    0 讨论(0)
  • 2020-12-04 06:23

    By Using underscore.js

    var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
    
    var resultArr = _.reject(arr,{id:3});
    
    console.log(resultArr);
    

    The result will be :: [{id:1name:'a'},{id:2,name:'c'}]

    0 讨论(0)
提交回复
热议问题