मान लें कि डुप्लीकेट के साथ ऑब्जेक्ट्स की हमारी सरणी निम्नलिखित है -
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.
आउटपुट
यहाँ, मेरी फ़ाइल का नाम डेमो158.js है। यह निम्नलिखित आउटपुट देगा -
PS C:\Users\Amit\JavaScript-code> node demo158.js
[
{ studentId: 101 },
{ studentId: 104 },
{ studentId: 106 },
{ studentId: 110 }
]