我有對象的數組這樣如何獲得UNIQ對象使用JavaScript或lodash
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"
}
]
};
};
,但我想通過電子郵件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: '[email protected]',
schemaName: 'Employee' },
{ _id: '577f69cc789df5ec1e995513',
firstName: 'Jeevan',
email: '[email protected]',
schemaName: 'Employee' } ]
我想這是沒有電子郵件ID的對象,我想只有它獨特的EMAILID的任何人可以幫助我在此對象。我希望Result包含沒有電子郵件ID的對象。結果應該是這樣的。
[ { _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' } ]
請給出一個例子整體結果應該是什麼樣子 – Arif
獨特的工作正常。您有一個未定義的電子郵件對象,以及您在其中設置的其他每封電子郵件的一個對象。正如阿里夫所說,如果你提供了預期的結果,我們可以幫助你如何得到它。 – Reza
你的意思是你不希望沒有電子郵件的對象被合併? –