Created
August 30, 2016 15:36
-
-
Save pchi/14ffaba7fc7c6d9a367d7423b9527a6c to your computer and use it in GitHub Desktop.
attempt to move dupes to end of list
This file contains hidden or 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
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