ধরুন, কিছু ইমেজের ডেটা অ্যারেতে আছে:
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 }];
আমাদের একটি ফাংশন লিখতে হবে যা এমন একটি অ্যারে নিবে। ফাংশনটি অ্যারেতে থেকে সেসব অবজেক্ট রিমুভ করবে যাদের 'image' প্রোপার্টির ভ্যালু ডুপ্লিকেট।
উদাহরণ
এটির কোড হবে:
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 } ]