如何使用javascript或lodash获取Uniq对象

时间:2016-07-26 05:28:17

标签: javascript node.js underscore.js lodash

我有像这样的对象数组

    var result={
            "employees": [
        {
            "_id": "5796e7a27d075bd0453b7751",
            "firstName": "Test4",
            "schemaName": "Employee"
        },
        {
            "_id": "5796e78e7d075bd0453b774f",
            "firstName": "Test 3",
            "schemaName": "Employee"
        },
        {
            "_id": "5790b203df5ad69025e8a20b",
            "email": "df@gmail.com",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Jeevan",
            "email": "ddd@asd.com",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Chethan",
            "email": "ddd@asd.com",
            "schemaName": "Employee"
        }
    ]
};
    };

但我希望通过电子邮件发送uniq对象。我正在使用lodash uniq,但它没有给出正确的结果。在这里我试过这段代码。

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

结果是这样的。

   [ { _id: '5796e7a27d075bd0453b7751',
    firstName: 'Test4',
    schemaName: 'Employee' },
  { _id: '5790b203df5ad69025e8a20b',
    email: 'df@gmail.com',
    schemaName: 'Employee' },
  { _id: '577f69cc789df5ec1e995513',
    firstName: 'Jeevan',
    email: 'ddd@asd.com',
    schemaName: 'Employee' } ]

我想要没有电子邮件ID的对象,而且我只想要唯一的邮件ID对象,任何人都可以帮助我。我希望结果包含没有电子邮件ID的对象。结果应该是这样的。

[ { _id: '5796e7a27d075bd0453b7751',
    firstName: 'Test4',
    schemaName: 'Employee' },
{ _id: '5796e78e7d075bd0453b774f',
    firstName: 'Test3',
    schemaName: 'Employee' },
  { _id: '5790b203df5ad69025e8a20b',
    email: 'df@gmail.com',
    schemaName: 'Employee' },
  { _id: '577f69cc789df5ec1e995513',
    firstName: 'Jeevan',
    email: 'ddd@asd.com',
    schemaName: 'Employee' } ]

3 个答案:

答案 0 :(得分:3)

这也可以成功。

_.uniqBy(result.employees, function(employee){return employee.email || employee._id;});

答案 1 :(得分:2)

我建议您使用自己的比较器来使用_.uniqWith

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": "df@gmail.com",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Jeevan",
            "__v": 0,
            "email": "fs@asf.com",
            "schemaName": "Employee"
        },
        {
            "_id": "577f69cc789df5ec1e995513",
            "firstName": "Chethan",
            "__v": 0,
            "email": "fs@asf.com",
            "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 :(得分:0)

我正在使用deepEquals算法,因此该代码变得更加通用。如果您只想通过电子邮件检查,只需更改下面的equals功能。

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 } ]