0

I am creating an array of objects, and I need to prevent entering the profile with the same name in array, but at the end I always get duplicated values:

let profileObject = {};
data.forEach(function (profile, index, array) {
  profileObject = {
    id: profile.id.S,
    name: profile.name.S
  };
});

Result will be:

profiles: [
  { id: '1', name: 'prof1' },
  { id: '1', name: 'prof1' },
  { id: '1', name: 'prof1' },
  { id: '2', name: 'prof2' },
  { id: '2', name: 'prof2' },
  { id: '2', name: 'prof2' }
]

But what I need is:

profiles: [
  { id: '1', name: 'prof1' },
  { id: '2', name: 'prof2' }
]

What I have tried is to check if the name is the same in forEach:

if (profile.name.S === profileObject.name) return;

Or

if (profileObject.includes(profile.name.S)) return;

But I just get the same result. I am new to javascript and cannot wrap my head around it.

0 Answers0