ধরুন, আমাদের কাছে এইরকম একটি অ্যারের মধ্যে কিছু চিত্র সম্পর্কিত কিছু ডেটা আছে -
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 }
]