2016-04-12 15 views
1

Ich habe zwei Arrays von ObjektenVergleichen Sie zwei Arrays von Objekten in Underscore.js

var arr1 = 
    [ 
    { 
     "lastInteracted": "2016-03-31T11:13:09.000Z", 
     "email": "[email protected]", 
     "interactionCount": 2 
    }, 
    { 
     "lastInteracted": "2016-03-31T21:06:19.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-29T11:15:41.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    } 
    ] 

und

var arr2 = 
[ 
    { 
     "lastInteracted": "2016-03-31T11:13:09.000Z", 
     "email": "[email protected]", 
     "interactionCount": 2 
    }, 
    { 
     "lastInteracted": "2016-03-31T21:06:19.000Z", 
     "email": "[email protected]", 
     "interactionCount": 4 
    }, 
    { 
     "lastInteracted": "2016-03-29T11:15:41.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 10 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 18 
    } 
] 

ich, dass diese zwei Arrays so zusammenführen möchten, wenn die E-Mail eines Objekts existiert in beiden dann Vergleichen Sie die interactionCount von arr1 mit arr2 sonst geben Sie die interactionCount entweder arr1 oder arr2 zurück.

Ergebnis wird

var result = [ 
    { 
     "lastInteracted": "2016-03-31T11:13:09.000Z", 
     "email": "[email protected]", 
     "interactionCount": 0 
    }, 
    { 
     "lastInteracted": "2016-03-31T21:06:19.000Z", 
     "email": "[email protected]", 
     "interactionCount": -4 
    }, 
    { 
     "lastInteracted": "2016-03-29T11:15:41.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-29T11:15:41.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 1 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 10 
    }, 
    { 
     "lastInteracted": "2016-03-24T10:02:29.000Z", 
     "email": "[email protected]", 
     "interactionCount": 18 
    } 
] 
+0

Haben Sie versucht das? http://StackOverflow.com/a/13514962/1702612 – Bikas

+0

Mögliches Duplikat von [Zusammenführung zweier Sammlungen mit Underscore.JS] (http://StackOverflow.com/questions/13514121/merging-two-collections-using-underscore-js) – alexmac

+0

Ich bin durch diese Lösungen gegangen, aber sie tun nicht, was ich brauche. Wenn Sie das Ergebnis-Array betrachten können, wird es vielleicht viel klarer, da ich nicht nur Duplikate eliminieren, sondern auch einen Unterschied der Werte erhalten will, wo immer die Duplikate vorhanden sind. –

Antwort

1

unterstreichen Verwenden Sie es wie folgt tun:

var arr1 = [{ 
 
    "lastInteracted": "2016-03-31T11:13:09.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 2 
 
}, { 
 
    "lastInteracted": "2016-03-31T21:06:19.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 1 
 
}, { 
 
    "lastInteracted": "2016-03-29T11:15:41.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 1 
 
}, { 
 
    "lastInteracted": "2016-03-24T10:02:29.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 1 
 
}] 
 
var arr2 = [{ 
 
    "lastInteracted": "2016-03-31T11:13:09.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 2 
 
}, { 
 
    "lastInteracted": "2016-03-31T21:06:19.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 4 
 
}, { 
 
    "lastInteracted": "2016-03-29T11:15:41.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 1 
 
}, { 
 
    "lastInteracted": "2016-03-24T10:02:29.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 1 
 
}, { 
 
    "lastInteracted": "2016-03-24T10:02:29.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 10 
 
}, { 
 
    "lastInteracted": "2016-03-24T10:02:29.000Z", 
 
    "email": "[email protected]", 
 
    "interactionCount": 18 
 
}] 
 

 
var ary = _.chain(arr1.concat(arr2))//use chain 
 
    .groupBy(function(d) { 
 
    return d.email; 
 
    })//grouping by email 
 
    .map(function(d) { 
 
    var last = _.last(d);//take the last in the group 
 
    var k = { 
 
     email: last.email, 
 
     lastInteracted: last.lastInteracted, 
 
     interactionCount: _.reduce(d, function(memo, d1) { 
 
     return memo + d1.interactionCount;//sum up interactionCount 
 
     }, 0) 
 
    }; 
 
    return k; 
 
    }).value() 
 

 
document.write('<pre>' + JSON.stringify(ary, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

Arbeits Geige here

+0

Das Jsfiddle funktioniert nicht. Es druckt nur einige Funktionen aus der Bibliothek. –

+0

Danke für das schnelle Update. Wenn Sie versuchen, interactionCounts zu subtrahieren, warum fügt es einfach '-' an die Summe der interactionCounts an? –

+0

Ich denke, die Frage ist das Hinzufügen von interactionCounts, warum subtrahieren Sie? – Cyril

Verwandte Themen