Skip to content

Instantly share code, notes, and snippets.

@pchi
Created August 30, 2016 15:36
Show Gist options
  • Save pchi/14ffaba7fc7c6d9a367d7423b9527a6c to your computer and use it in GitHub Desktop.
Save pchi/14ffaba7fc7c6d9a367d7423b9527a6c to your computer and use it in GitHub Desktop.
attempt to move dupes to end of list
const someCollection = [{
results: [
{
name: 'alpha',
region : {
region_id : 1234
}
},
{
name: 'beta',
region : {
region_id : 1
}
},
{
name: 'this should move to bottom',
region : {
region_id : 1234
}
}
]
}];
function transformCollection(someCollection) {
// if collection has results
if (someCollection[0].results) {
// create hashmap
let exists = {};
// create a new list
let parsedList = [];
// create duped list
let dupedList = [];
someCollection[0].results.forEach(item => {
if (exists[item.region.region_id]) {
dupedList.push(item);
} else {
exists[item.region.region_id] = true;
parsedList.push(item);
}
});
console.log(parsedList.concat(dupedList));
return parsedList.concat(dupedList);
}
}
transformCollection(someCollection);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment