如何从 intl-tel-input 中删除重复的国家/地区

Posted

技术标签:

【中文标题】如何从 intl-tel-input 中删除重复的国家/地区【英文标题】:How to remove a duplicate country from intl-tel-input 【发布时间】:2021-12-02 15:46:49 【问题描述】:

如何去除首选国家的重复。我需要在下拉列表中添加所有非洲国家作为首选国家。

我使用的代码。

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 filteredArray = Array.from(new Set(preferredCountries)) 【参考方案1】:

您可以在这里使用Set 获取唯一值,然后使用... spread 运算符返回数组:

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; 

另一个例子:

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; 

【讨论】:

【参考方案2】:

你可以通过不同的方式避免数组的重复

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);

另一种方法是

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);

您还可以使用 forEach() 和 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);

【讨论】:

以上是关于如何从 intl-tel-input 中删除重复的国家/地区的主要内容,如果未能解决你的问题,请参考以下文章

国家代码 intl-tel-input 后​​未删除数字 0

jquery 从对象中获取元素 - intl-tel-input 从国家数据中获取拨号代码

PHP使用intl-tel-input获取电话号码

如何使用 intl-tel-input 格式化数字?

如何在IONIC 3中为intl-tel-input创建IONIC组件

Laravel 5.6:与 App.js 的 intl-tel-input 兼容性问题