उदाहरण
Set का उपयोग करके यूनिक प्रॉपर्टी के आधार पर डुप्लिकेट हटाएं।
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
आउटपुट
PS C:\Users\Amit\JavaScript-code> node demo158.js
[ { studentId: 101 }, { studentId: 104 }, { studentId: 106 }, { studentId: 110 } ]