Skip to content Skip to sidebar Skip to footer

Removing Duplicates With Lodash

I've got the following array: data = [{ name: 'Robert', urls: [{ provider: 'facebook', url: 'http://twitter.com/rob' }] }, { name: 'Robert', urls: [{ provider

Solution 1:

You can use lodash#groupBy to group the collection by name. Rebuild the group into an array using lodash#map. Use lodash#flatMap to obtain all urls into a flattened format, so we can remove all the duplicates through lodash#uniqWith that has a lodash#isEqual comparator function.

var result = _(data)
  .groupBy('name')
  .map((group, name) => ({
    name,
    urls: _(group).flatMap('urls').uniqWith(_.isEqual).value()
  }))
  .value();

var data = [{
  name: "Robert",
  urls: [{
    provider: "facebook",
    url: "http://twitter.com/rob"
  }]
}, {
  name: "Robert",
  urls: [{
    provider: "youtube",
    url: "http://youtube.com/robs"
  }]
}, {
  name: "Linda is a perfect duplicate",
  urls: [{
    provider: "youtube",
    url: "http://youtube.com/lindaTube"
  }]
}, {
  name: "Linda is a perfect duplicate",
  urls: [{
    provider: "youtube",
    url: "http://youtube.com/lindaTube"
  }]
}];

var result = _(data)
  .groupBy('name')
  .map((group, name) => ({
    name,
    urls: _(group).flatMap('urls').uniqWith(_.isEqual).value()
  }))
  .value();
  
console.log(result);
.as-console-wrapper{min-height:100%;top:0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

<!--
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
-->

Solution 2:

How about it?

var data = [{
  name: "Robert",
  urls: [{
    provider: "facebook",
    url: "http://twitter.com/rob"
  }]
}, {
  name: "Robert",
  urls: [{
    provider: "youtube",
    url: "http://youtube.com/robs"
  }]
}, {
  name: "Linda is a perfect duplicate",
  urls: [{
    provider: "youtube",
    url: "http://youtube.com/lindaTube"
  }]
}, {
  name: "Linda is a perfect duplicate",
  urls: [{
    provider: "youtube",
    url: "http://youtube.com/lindaTube"
  }]
}]

var newdata = _.reduce(data, (result, value) => 
  _.values(
    _.mergeWith(
      _.keyBy(result, 'name'), 
      _.keyBy([value], 'name'), 
      (obj, src) => 
        _.isArray(obj) ? 
          _.values(_.merge(_.keyBy(obj, 'provider'), _.keyBy(src, 'provider'))) : 
          undefined   
    )
  ), []
);

console.log(newdata);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Post a Comment for "Removing Duplicates With Lodash"