2016-07-26 6 views
1

ich das Array von Objekten wie dieseWie die Uniq Objekte mit Hilfe von JavaScript erhalten oder 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" 
     } 
    ] 
}; 
    }; 

aber ich möchte uniq Objekte per E-Mail habe. Ich benutze lodash uniq, aber es gibt kein richtiges Ergebnis. Hier habe ich diesen Code ausprobiert.

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

Das Ergebnis kommt so.

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

Ich mag die Objekte, die E-Mail-IDs nicht mit und ich will nur Objekte, die die einzigartigen EMAILID sind kann jemand mir auf diesem helfen. Ich möchte, dass das Ergebnis die Objekte enthält, die auch keine E-Mail-ID haben. Das Ergebnis sollte so sein.

[ { _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

geben Sie bitte ein Beispiel, wie das Gesamtergebnis aussehen sollte – Arif

+0

Unique funktioniert ordnungsgemäß. Sie haben ein Objekt für E-Mail undefiniert und ein Objekt für jede andere E-Mail, die Sie in Ihrem Set haben. Wie Arif sagte, wenn Sie das erwartete Ergebnis liefern, können wir Ihnen helfen, wie Sie es bekommen. – Reza

+0

Meinst du, dass die Objekte ohne E-Mail nicht zusammengeführt werden sollen? –

Antwort

3

Dies könnte es machen.

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

würde ich die Verwendung von _.uniqWith für diese mit Ihrem eigenen Komparator empfehlen.

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 danke für deine Antwort. Haben wir andere Möglichkeiten, dies zu tun? Ich benutze Lodash 3.10 seine Unterstützung dieser Funktion _.uniqWith nicht. Ich habe keine Erlaubnis, das Lodash so zu aktualisieren. – Jeevan

+0

In diesem Fall scheint die Antwort von @ season angemessen zu sein. –

Verwandte Themen