다음이 중복된 객체의 배열이라고 가정해 보겠습니다. -
var studentDetails=[
{studentId:101},
{studentId:104},
{studentId:106},
{studentId:104},
{studentId:110},
{studentId:106},
] 아래 코드와 같이 세트 개념을 사용하여 중복을 제거하십시오 -
예시
var studentDetails=[
{studentId:101},
{studentId:104},
{studentId:106},
{studentId:104},
{studentId:110},
{studentId:106},
]
const distinctValues = new Set
const withoutDuplicate = []
for (const tempObj of studentDetails) {
if (!distinctValues.has(tempObj.studentId)) {
distinctValues.add(tempObj.studentId)
withoutDuplicate.push(tempObj)
}
}
console.log(withoutDuplicate); 위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -
node fileName.js.
출력
여기에서 내 파일 이름은 demo158.js입니다. 이것은 다음과 같은 출력을 생성합니다 -
PS C:\Users\Amit\JavaScript-code> node demo158.js
[
{ studentId: 101 },
{ studentId: 104 },
{ studentId: 106 },
{ studentId: 110 }
]