Lodash remove duplicates from array

后端 未结 7 653
慢半拍i
慢半拍i 2020-11-29 17:12

This is my data:

[
    {
        url: \'www.example.com/hello\',
        id: \"22\"    
    },
    {
        u         


        
相关标签:
7条回答
  • 2020-11-29 17:29

    With lodash version 4+, you would remove duplicate objects either by specific property or by the entire object like so:

    var users = [
      {id:1,name:'ted'},
      {id:1,name:'ted'},
      {id:1,name:'bob'},
      {id:3,name:'sara'}
    ];
    var uniqueUsersByID = _.uniqBy(users,'id'); //removed if had duplicate id
    var uniqueUsers = _.uniqWith(users, _.isEqual);//removed complete duplicates
    

    Source:https://www.codegrepper.com/?search_term=Lodash+remove+duplicates+from+array

    0 讨论(0)
  • 2020-11-29 17:32

    For a simple array, you have the union approach, but you can also use :

    _.uniq([2, 1, 2]);
    
    0 讨论(0)
  • 2020-11-29 17:35

    You can also use unionBy for 4.0.0 and later, as follows: let uniques = _.unionBy(data, 'id')

    0 讨论(0)
  • 2020-11-29 17:37

    Simply use _.uniqBy(). It creates duplicate-free version of an array.

    This is a new way and available from 4.0.0 version.

    _.uniqBy(data, 'id');
    

    or

    _.uniqBy(data, obj => obj.id);
    
    0 讨论(0)
  • 2020-11-29 17:41

    You could use lodash method _.uniqWith, it is available in the current version of lodash 4.17.2.

    Example:

    var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
    
    _.uniqWith(objects, _.isEqual);
    // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
    

    More info: https://lodash.com/docs/#uniqWith

    0 讨论(0)
  • 2020-11-29 17:47

    Or simply Use union, for simple array.

    _.union([1,2,3,3], [3,5])
    
    // [1,2,3,5]
    
    0 讨论(0)
提交回复
热议问题