2016-07-26 27 views
1

我有對象的數組這樣如何獲得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' } ] 
+0

請給出一個例子整體結果應該是什麼樣子 – Arif

+0

獨特的工作正常。您有一個未定義的電子郵件對象,以及您在其中設置的其他每封電子郵件的一個對象。正如阿里夫所說,如果你提供了預期的結果,我們可以幫助你如何得到它。 – Reza

+0

你的意思是你不希望沒有電子郵件的對象被合併? –

回答

3

這可能使這一點。

_.uniqBy(result.employees, function(employee){return employee.email || employee._id;}); 
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": "[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>

+0

@Rouque謝謝你的回答。我們有其他的選擇嗎?我使用lodash 3.10它不支持_.uniqWith功能。我沒有權限更新lodash。 – Jeevan

+0

在這種情況下@季節的答案看起來合適。 –

相關問題