removing duplicates in array javascript
let arr = [1,2,3,1,1,1,4,5]
let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
console.log(filtered) // [1,2,3,4,5]
removing duplicates in array javascript
let arr = [1,2,3,1,1,1,4,5]
let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
console.log(filtered) // [1,2,3,4,5]
remove duplicates from array javascript
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
remove duplicates array javascript
function uniqByKeepFirst(a, key) {
let seen = new Set();
return a.filter(item => {
let k = key(item);
return seen.has(k) ? false : seen.add(k);
});
}
function uniqByKeepLast(a, key) {
return [
...new Map(
a.map(x => [key(x), x])
).values()
]
}
//
data = [
{a:1, u:1},
{a:2, u:2},
{a:3, u:3},
{a:4, u:1},
{a:5, u:2},
{a:6, u:3},
];
console.log(uniqByKeepFirst(data, it => it.u))
console.log(uniqByKeepLast(data, it => it.u))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us