从数组中移除/过滤重复记录 - JavaScript?
假设我们的记录包含以下一些重复值 −
var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ];
例
要移除重复项,请使用 Set() 概念。以下为代码 −
var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; function removeOrFilterNationality(objectOfNationality) { return Array.from(new Set(objectOfNationality.map(tempObject => tempObject.nationality))); } console.log(removeOrFilterNationality(objectOfNationality));
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,我的文件名是 demo228.js。
输出
输出如下 −
PS C:\Users\Amit\JavaScript-code> node demo228.js [ 'Indian', 'American', 'Emirati' ]
广告