Home > Software engineering >  How to remove a duplicate country from intl-tel-input
How to remove a duplicate country from intl-tel-input

Time:10-14

How to remove the repetition of preferred countries. I need to add all the African countries as preferred countries in the dropdown list.

code i used for that.

preferredCountries: [ "gh", "dz","ao","bj","bw","bf","bi","cm","cf","td","km","cd","dj","eg","gq","er","sz","et","ga","gm","gq","gw","ke","ls","lr","ly","mg","mw","ml","mr","mu","ma","mz","na","ne","ng","rw","sn","sc","sl","so","za","ss","sd","tz","tg","tn","ug","zm","zw" ],
But this same country's all are repeating in the list again. How can I remove the repetition.

CodePudding user response:

You can use a Set to get the unique values here, then use the ... spread operator to return to an array:

preferredCountries = [ "gh", "dz","ao","bj","bw","bf","bi","cm","cf","td","km","cd","dj","eg","gq","er","sz","et","ga","gm","gq","gw","ke","ls","lr","ly","mg","mw","ml","mr","mu","ma","mz","na","ne","ng","rw","sn","sc","sl","so","za","ss","sd","tz","tg","tn","ug","zm","zw" ];

const uniqueCountries = [...new Set(preferredCountries)];

console.log({ uniqueCountries });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Another example:

const values = [ 'a', 'a', 'a', 'b', 'b', 'b', 'c' ];

// Use spread operator and new Set()
let uniqueValues = [...new Set(values)];

console.log({ uniqueValues });

// Or use Array.from() and new Set()
uniqueValues = Array.from(new Set(values));

console.log({ uniqueValues });
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

You can avoid duplication of array by different ways

let preferredCountries: [ "gh", "dz","ao","bj","bw","bf","bi","cm","cf","td","km","cd","dj","eg","gq","er","sz","et","ga","gm","gq","gw","ke","ls","lr","ly","mg","mw","ml","mr","mu","ma","mz","na","ne","ng","rw","sn","sc","sl","so","za","ss","sd","tz","tg","tn","ug","zm","zw" ],

let NewpreferredCountries= [...new Set(chars)];

console.log(NewpreferredCountries);

Another Method is

let preferredCountries: [ "gh", "dz","ao","bj","bw","bf","bi","cm","cf","td","km","cd","dj","eg","gq","er","sz","et","ga","gm","gq","gw","ke","ls","lr","ly","mg","mw","ml","mr","mu","ma","mz","na","ne","ng","rw","sn","sc","sl","so","za","ss","sd","tz","tg","tn","ug","zm","zw" ],

let NewpreferredCountries= preferredCountries.filter((c, index) => {
    return preferredCountries.indexOf(c) === index;
});

console.log(NewpreferredCountries);

You can also remove duplicates from an array using forEach() and include()

let preferredCountries: [ "gh", "dz","ao","bj","bw","bf","bi","cm","cf","td","km","cd","dj","eg","gq","er","sz","et","ga","gm","gq","gw","ke","ls","lr","ly","mg","mw","ml","mr","mu","ma","mz","na","ne","ng","rw","sn","sc","sl","so","za","ss","sd","tz","tg","tn","ug","zm","zw" ],

let NewpreferredCountries= [];
preferredCountries.forEach((c) => {
    if (!NewpreferredCountries.includes(c)) {
        NewpreferredCountries.push(c);
    }
});

console.log(NewpreferredCountries);
  • Related