Answers for "how to filter unique element in javascript"

16

javascript find unique values in array

// usage example:
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); 

// unique is ['a', 1, 2, '1']
Posted by: Guest on February-28-2020
0

javascript remove uniques from array

function getNotUnique(array) {
    var map = new Map();
    array.forEach(a => map.set(a, (map.get(a) || 0) + 1));
    return array.filter(a => map.get(a) > 1);
}

console.log(getNotUnique([1, 2, 2, 4, 4])); //[2, 2, 4, 4]
console.log(getNotUnique([1, 2, 3] )); //[]
Posted by: Guest on November-09-2020

Code answers related to "how to filter unique element in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language