“cara menghapus duplikat dari array di javascript” Kode Jawaban

JS menghapus duplikat dari array

const names = ['John', 'Paul', 'George', 'Ringo', 'John'];

let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
Wandering Weevil

Menghapus duplikat di 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]
Runtime Terror

JavaScript Hapus duplikat dalam dua array

array1 = array1.filter(function(val) {
  return array2.indexOf(val) == -1;
});
// Or, with the availability of ES6:

array1 = array1.filter(val => !array2.includes(val));
MM.Mirzaei.Dev

cara menghapus duplikat dari array di 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 ]
Chetan Nada

JavaScript Hapus duplikat dari array

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 ]
Rashid Siddique

Hapus duplikasi dari array di JavaScript

const names = ["Alvi", "Arham", "Talha", "Safi", "Sameer", "Nazmi", "labli", "Arham", "Talha", "Labiba", "Tabassum", "Adiba"];
function removeDuplication(names) {
    const unique = [];
    for (element of names) {
        if (unique.indexOf(element) == -1) {
            unique.push(element)
        }
    }
    return unique;
}
console.log(removeDuplication(names));
//Output:
/* [
    'Alvi',
    'Arham',  'Talha',
    'Safi',   'Sameer',
    'Nazmi',  'labli',
    'Labiba', 'Tabassum',
    'Adiba'
] */
Ariful Islam Adil(Code Lover)

Jawaban yang mirip dengan “cara menghapus duplikat dari array di javascript”

Pertanyaan yang mirip dengan “cara menghapus duplikat dari array di javascript”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya