Last active
April 4, 2016 00:00
-
-
Save toranb/b661b53f9ab277ef8d4041cebe770414 to your computer and use it in GitHub Desktop.
a simple array helper to return a new collection of data
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
export function uniq(first, second) { | |
let ret = []; | |
const collection = first.concat(second); | |
collection.forEach((k) => { | |
var index = ret.findIndex((item) => item.id === k.id); | |
if (index === -1) { | |
ret.push(k); | |
}else{ | |
ret.replace(index, 1, k); | |
} | |
}); | |
return ret; | |
} | |
export function remove(collection, id) { | |
var index = collection.findIndex((item) => item.id === id); | |
return [ | |
...collection.slice(0, index), | |
...collection.slice(index + 1) | |
]; | |
} | |
//100% certain someone could do this better with lodash :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment