0

i have an Array of Objects each Objects have an (Id,num) properties

const arrayofDuplicates = [
    {num:1,id:25},
    {num:1,id:25},
    {num:2,id:2}, 
    {num:3,id:3}, 
    {num:1,id:25},
];

now what i want to do is to Remove the Duplicate Objects from my Array so i created a function that loops over the Array and filter the duplicates and return a new Array without the Duplicates the Problem i have here is that it Returns the Same Array with the Duplicates like if nothing changed

const FindDuplicate =(duplicareArr,layerId)=>{
  const duplicateVal=duplicareArr.find((el)=>el.id===layerId)
  console.log(duplicateVal)
  return  duplicateVal 
}
const values = arrayofDuplicates.map((el,index,arr)=>{

    const FilteredObject= FindDuplicate(arr,el.id)
    return FilteredObject;
}
  • Why do you assume that `map` would remove elements from the array? – t.niese May 13 '22 at 19:08
  • Why not use the ```filter``` array method? – Sean May 13 '22 at 19:10
  • `map()` always returns an array that's the same length as the original. Use either `filter()` or `reduce()` to return a different size array. – Barmar May 13 '22 at 19:10
  • What does `FindDuplicate()` do, and why are you calling it in the mapping function? – Barmar May 13 '22 at 19:11
  • @Barma findDuplicate find the first object with the Id provided as a parameter / because the Map Function Loops over the Array what i thought is if i call it there it will Find the first unique Object and return and then the object would be in the new Array Returned from the Map Function – BigDaddyTrump May 13 '22 at 19:25
  • @t.niese that's not what i thought here is what i thought of Map Function Loops over the Array what i thought is if i call the findDuplicate it will Find the first unique Object and return it and then the object would be in the new Array Returned from the Map Function – BigDaddyTrump May 13 '22 at 19:28
  • You're not using the result of `FindDuplicate()`. What does `FindDuplicateNum()` do? That used to be the name of the function before you renamed it to `FindDuplicate`. – Barmar May 13 '22 at 19:32
  • @SeanLawton i got the same Problem i mentioned in my post – BigDaddyTrump May 13 '22 at 19:32
  • @Barmar `findDuplicateNum` is A typo it's suppose to be `findDuplicate` that;s why i changed it – BigDaddyTrump May 13 '22 at 19:36
  • Why do you call `FindDuplicate()` twice? You call it once without using the result, and then again to set `FilteredObject`. – Barmar May 13 '22 at 19:38
  • i am actually using it but There was a mistake in the post i am using it's value in the Original Code but still got the same problem Apparently using Find Method to Filter the Objects is not the Solution still wanna know why and i am Really srry for the confusion i caused you – BigDaddyTrump May 13 '22 at 20:45

0 Answers0