1

i am having the Array of objects like this

    var result={
            "employees": [
        {
            "_id": "5796e7a27d075bd0453b7751",
            "firstName": "Test4",
            "schemaName": "Employee"
        },
        {
            "_id": "5796e78e7d075bd0453b774f",
            "firstName": "Test 3",
            "schemaName": "Employee"
        },
        {
            "_id": "5790b203df5ad69025e8a20b",
            "email": "[email protected]",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Jeevan",
            "email": "[email protected]",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Chethan",
            "email": "[email protected]",
            "schemaName": "Employee"
        }
    ]
};
    };

but i want uniq objects by email. i am using lodash uniq but its Not giving proper Result. here i tried this code.

var combined=result.employees.map(function(employee){
    employee.schemaName='Employee';
    return employee;
});
combined = _.uniq(combined, 'email');
console.log(combined);

The Result is coming like this.

   [ { _id: '5796e7a27d075bd0453b7751',
    firstName: 'Test4',
    schemaName: 'Employee' },
  { _id: '5790b203df5ad69025e8a20b',
    email: '[email protected]',
    schemaName: 'Employee' },
  { _id: '577f69cc789df5ec1e995513',
    firstName: 'Jeevan',
    email: '[email protected]',
    schemaName: 'Employee' } ]

i want the objects which are not having email ids and i want only objects which are unique emailid's can anybody help me on this. I want the Result contain the objects Which are not having email id also. The Result should be like this.

[ { _id: '5796e7a27d075bd0453b7751',
    firstName: 'Test4',
    schemaName: 'Employee' },
{ _id: '5796e78e7d075bd0453b774f',
    firstName: 'Test3',
    schemaName: 'Employee' },
  { _id: '5790b203df5ad69025e8a20b',
    email: '[email protected]',
    schemaName: 'Employee' },
  { _id: '577f69cc789df5ec1e995513',
    firstName: 'Jeevan',
    email: '[email protected]',
    schemaName: 'Employee' } ]
8
  • please give an example what the overall result should look like Commented Jul 26, 2016 at 5:31
  • Unique is working correctly. you have one object for email undefined, and one object of every other email that your have in your set. As Arif said, If you provide the expected result we can help you with how to get it. Commented Jul 26, 2016 at 5:33
  • Do you mean you don't want the objects without email to be merged? Commented Jul 26, 2016 at 5:36
  • @Arif ya i have updated Result please Check. Commented Jul 26, 2016 at 5:39
  • @Roque i want objects without email also to be merged Commented Jul 26, 2016 at 5:40

3 Answers 3

3

This could make it too.

_.uniqBy(result.employees, function(employee){return employee.email || employee._id;});
Sign up to request clarification or add additional context in comments.

Comments

2

I would recommend the use of _.uniqWith for this with your own comparator.

var result = {
    "employees": [
        {
            "_id": "5796e7a27d075bd0453b7751",
            "firstName": "Test4",
            "lastName": "T",
            "__v": 0,
            "schemaName": "Employee"
        },
        {
            "_id": "5796e78e7d075bd0453b774f",
            "firstName": "Test 3",
            "lastName": "T",
            "__v": 0,
            "schemaName": "Employee"
        },
        {
            "_id": "5796e77e7d075bd0453b774d",
            "firstName": "Test 2",
            "lastName": "T",
            "__v": 0,
            "documents": [],
            "schemaName": "Employee"
        },
        {
            "_id": "5796e7707d075bd0453b774b",
            "firstName": "Test1",
            "lastName": "T",
            "__v": 0,
            "schemaName": "Employee"
        },
        {
            "_id": "5790b203df5ad69025e8a20b",
            "firstName": "Ganesh",
            "lastName": "dsf",
            "__v": 0,
            "email": "[email protected]",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Jeevan",
            "__v": 0,
            "email": "[email protected]",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Chethan",
            "__v": 0,
            "email": "[email protected]",
            "schemaName": "Employee"
        }
    ]
};

// Create a lodash chain from the employees.
combined = _.uniqWith(result.employees, function(e1, e2){
  return e1.email && e1.email === e2.email;
});

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

2 Comments

@Rouque thanks for your answer. do we have any other option to do this. i am using lodash 3.10 its not Supporting this _.uniqWith function. i dont have permission to update the lodash so.
In that case @season's answer looks appropriate.
0

I'm using a deepEquals algorithm so this code becomes more general. If you want to check only by email, just change the equals function below.

const equals =
  // Naïve deepEquals, only for demo. Doesn't work if keys orders vary.
  (obj1, obj2) => JSON.stringify(obj1) === JSON.stringify(obj2);

  // A real deepEquals:
  // https://www.npmjs.com/package/fast-deep-equal

const uniq = (el1, index1, array) =>
  array.every((el2, index2) => index1 <= index2 || !equals(el1, el2));

[{ a: 1, b: 2 }, { c: 3 }, { a: 1, b: 2 }, { d: 4 }, { a: 1, b: 2 }].filter(uniq);
// Result: [ { a: 1, b: 2 }, { c: 3 }, { d: 4 } ]

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.