6

I'm trying to remove duplicate items using lodash.js but I can't make it works properly.

This is the structure of an object in the array:

{
   label: 'tagA',
   value: 1
}

So let say I have this array:

var objectsArray = [
  {
    label: 'tagA',
    value: 1
  },
  {
    label: 'tagB',
    value: 2
  },
  {
    label: 'tagC',
    value: 3
  },
  {
    label: 'tagB',
    value: 4
  },
  {
    label: 'tagB',
    value: 5
  },
];

I made this piece of code with _.uniqBy() function from lodash.js to try to remove the elements of array with the same labels, but it dosn't work as I expected:

    var uniq = _.uniqBy(objectsArray, function(o){
        return o.label;
    });

I based on some sample found here and there and lodash documentation of course but I have a lack of knowledge in this regard so any help it will super appreciate it.

Thanks.

1
  • it doesn't work as expected How do you expect it to work, and how does it work? Commented May 21, 2016 at 15:18

3 Answers 3

11

Make sure that you use proper namings, that code works for me:

    var arr = [
                {
                  label: 'tagA',
                  value: 1
                },
                {
                  label: 'tagB',
                  value: 2
                },
                {
                  label: 'tagC',
                  value: 3
                },
                {
                  label: 'tagB',
                  value: 4
                },
                {
                  label: 'tagB',
                  value: 5
                },
              ];

    var uniq = _.uniqBy(arr, function(o){
        return o.label;
    });

    console.log(uniq); // >> Returned an array with first 3 objects from array arr
Sign up to request clarification or add additional context in comments.

6 Comments

I think he missed the array name in the first .uniqBy() argument
No, he passed objectsArray.
I see. But I don't see if he declared it, I see only unnamed array above.
It's a safe assumption that he did. If you think he might not have, you should leave a comment confirming that.
@torazaburo that's just only way to fail here, or maybe he forgot to import lib
|
2

If you want to make sure you can use uniqWith();

This works for me

var data = [
  {
    label: 'tagA',
    value: 1
  },
  {
    label: 'tagB',
    value: 2
  },
  {
    label: 'tagC',
    value: 3
  },
  {
    label: 'tagB',
    value: 4
  },
  {
    label: 'tagB',
    value: 5
  },
];

var filtered = _.uniqWith(data, function(first, second){

    return first.label === second.label
});

Comments

0

I think the second example is just what you need uniqBy:

// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// → [{ 'x': 1 }, { 'x': 2 }]

1 Comment

Thanks for your effort I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.