Answers for "how to avoid pushing duplicate values in array in javascript"

5

javascript to remove duplicates from an array

uniqueArray = a.filter(function(item, pos) {
    return a.indexOf(item) == pos; 
})
Posted by: Guest on October-22-2020
0

how to remove duplicates from array in javascript

// how to remove duplicates from array in javascript
// 1. filter()
let num = [1, 2, 3, 3, 4, 4, 5, 5, 6];
let filtered = num.filter((a, b) => num.indexOf(a) === b)
console.log(filtered);
// Result: [ 1, 2, 3, 4, 5, 6 ]

// 2. Set()
const removeDuplicates = (arr) => [...new Set(arr)];
console.log(removeDuplicates([1, 2, 3, 3, 4, 4, 5, 5, 6]));
// Result: [ 1, 2, 3, 4, 5, 6 ]
Posted by: Guest on January-07-2022
0

prevent duplicate entries in javascript array

if (array.indexOf(value) === -1) array.push(value);
Posted by: Guest on March-01-2021
0

how to remove duplicate values in array javascript

var car = ["Saab","Volvo","BMW","Saab","BMW",];
var cars = [...new Set(car)]
document.getElementById("demo").innerHTML = cars;
Posted by: Guest on April-06-2021

Code answers related to "how to avoid pushing duplicate values in array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language