Skip to content

Instantly share code, notes, and snippets.

@1ay1
Created February 7, 2022 09:00
Show Gist options
  • Save 1ay1/6eebf40f5b78155a5d55052a6a079dea to your computer and use it in GitHub Desktop.
Save 1ay1/6eebf40f5b78155a5d55052a6a079dea to your computer and use it in GitHub Desktop.
// def merge_posts(list_of_posts: List[List[Post]]) -> List[Post]
function merge_posts(list_of_posts) {
let posts = [];
list_of_posts.map(val => {
posts.concat(val);
});
//only uniques
posts = posts.filter((val, index) => posts.indexOf(val) === index);
//sort the thing
posts.sort((l, r) => {
if(l.created_at > r.created_at) {
return -1;
} else {
if(l.id == r.created_at) {
//break the tie
if(l.id > r.id) return -1;
else return 1;
}
return 1;
}
})
let response = posts.map((val, index) => {
return {
id: val.id,
description: val.description,
image: val.image,
created_at: cal.created_at,
}
})
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment