Lodash remove duplicates from array

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

This is my data:

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


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

    _.unique no longer works for the current version of Lodash as version 4.0.0 has this breaking change. The functionality of _.unique was splitted into _.uniq, _.sortedUniq, _.sortedUniqBy, and _.uniqBy.

    You could use _.uniqBy like this:

    _.uniqBy(data, function (e) {
      return e.id;
    });
    

    ...or like this:

    _.uniqBy(data, 'id');
    

    Documentation: https://lodash.com/docs#uniqBy


    For older versions of Lodash (< 4.0.0 ):

    Assuming that the data should be uniqued by each object's id property and your data is stored in data variable, you can use the _.unique() function like this:

    _.unique(data, function (e) {
      return e.id;
    });
    

    Or simply like this:

    _.uniq(data, 'id');
    
    0 讨论(0)
提交回复
热议问题