0

I have an array of object which looks like

const test = [{'activity': {'Name': 'Peter'}}, {'activity': {'Name': 'Peter'}},
{'activity': {'Name': 'John'}},{'activity': {'Name': 'Derek'}}]

Here I have used this function

const uniqBuyingSessions = _.uniqBy(
      buyingSessions, 'Name'
    )

It returns only the first object, in any case .

So, I was expecting the result to be

const test = [{'activity': {'Name': 'Peter'}},
{'activity': {'Name': 'John'}},{'activity': {'Name': 'Derek'}}]

So, Is there anything that I am doing wrong?

2 Answers 2

3

There is no Name property on your objects.

lodash traverses every object and try to read Name, which resolves to undefined. Since it will return undefined for every object, only the first one is returned (standard uniq behavior).

Name is actually a nested property (activity.Name).

I think this should work:

const uniqBuyingSessions = _.uniqBy(
  buyingSessions, 'activity.Name'
);
Sign up to request clarification or add additional context in comments.

Comments

0

const test = [
{ 'activity': { 'Name': 'Peter' } },
{ 'activity': { 'Name': 'Peter' } },
{ 'activity': { 'Name': 'John' } },
{ 'activity': { 'Name': 'Derek' } }
];

const uniq = _.uniqBy(test, item => {
return item.activity.Name;
});
console.log(uniq);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

Comments

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.