从数组中删除/过滤重复记录-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' ]
猜你喜欢