Created
February 11, 2014 10:22
-
-
Save anonymous/8932420 to your computer and use it in GitHub Desktop.
Calculate the difference between two lists of posts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash') | |
var posts_one = [ | |
{ id: 1, title: 'foo'} | |
, { id: 2, title: 'bar'} | |
, { id: 2, title: 'bar'} | |
, { id: 3, title: 'baz'} | |
, { id: 4, title: 'merp'} | |
, { id: 4, title: 'merp'} | |
, { id: 5, title: 'flakes'} | |
] | |
var posts_two = [ | |
{ id: 1, title: 'foo'} | |
, { id: 2, title: 'bar'} | |
, { id: 3, title: 'baz'} | |
, { id: 3, title: 'baz'} | |
, { id: 4, title: 'merp'} | |
, { id: 5, title: 'flakes'} | |
, { id: 6, title: 'new'} | |
, { id: 7, title: 'neu'} | |
] | |
var ids_one = _.uniq(_.map(posts_one, function(p) { | |
return p.id | |
})) | |
var ids_two = _.uniq(_.map(posts_two, function(p) { | |
return p.id | |
})) | |
var ids_diff = _.difference(ids_two, ids_one) | |
var posts_diff = _.filter(posts_two, function(p) { | |
if(_.contains(ids_diff, p.id)) { | |
return p | |
} | |
}) | |
console.log('then', ids_one) | |
console.log('now', ids_two) | |
console.log('diff', ids_diff) | |
console.log('posts', posts_diff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment