मान लीजिए, हमारे पास इस तरह की एक सरणी में कुछ छवियों के संबंध में कुछ डेटा है -
const arr = [{
'image': "jv2bcutaxrms4i_img.png",
'gallery_image': true
},
{
'image': "abs.png",
'gallery_image': true
},
{
'image': "acd.png",
'gallery_image': false
},
{
'image': "jv2bcutaxrms4i_img.png",
'gallery_image': true
},
{
'image': "abs.png",
'gallery_image': true
},
{
'image': "acd.png",
'gallery_image': false
}]; हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक ऐसी सरणी लेता है।
हमारे फ़ंक्शन को उन वस्तुओं को सरणी से हटा देना चाहिए जिनमें 'छवि' संपत्ति के लिए डुप्लिकेट मान हैं।
उदाहरण
इसके लिए कोड होगा -
const arr = [{
'image': "jv2bcutaxrms4i_img.png",
'gallery_image': true
},
{
'image': "abs.png",
'gallery_image': true
},
{
'image': "acd.png",
'gallery_image': false
},
{
'image': "jv2bcutaxrms4i_img.png",
'gallery_image': true
},
{
'image': "abs.png",
'gallery_image': true
},
{
'image': "acd.png",
'gallery_image': false
}];
const buildUnique = (arr = []) => {
const unique = [];
arr.forEach(obj => {
let found = false;
unique.forEach(uniqueObj => {
if(uniqueObj.image === obj.image) {
found = true;
};
});
if(!found){
unique.push(obj);
};
});
return unique;
};
console.log(buildUnique(arr)); आउटपुट
और कंसोल में आउटपुट होगा -
[
{ image: 'jv2bcutaxrms4i_img.png', gallery_image: true },
{ image: 'abs.png', gallery_image: true },
{ image: 'acd.png', gallery_image: false }
]