2

I want to get unique entries from array which is showing different behaviour on 1d array and object array , I want to get unique entry from the object array Similar question has already been asked Get unique results from JSON array using jQuery and Array unique values but they are different from my case , I just want to ask why 1d array and object array behave differently when they are converted to set and back to array to get unique entries and what is the mistake I did

My json array is like below

    let arr=[{description: "d1", item_id: 298237, id: 298237, show_tag: 0, tags: "6,7,15,16,20"},
    {description: "d2", item_id: 297743, id: 297743, show_tag: 0, tags: "5,7,15,16,20"},
    {description: "d1", item_id: 298237, id: 298237, show_tag: 0, tags: "6,7,15,16,20"},
    {description: "d2", item_id: 297743, id: 297743, show_tag: 0, tags: "5,7,15,16,20"}
    ]
 let u=Array.from(new Set(arr));
 console.log(u);
    
 //it works for normal 1d array
 let arr1=[2,3,5,6,2,3];
 let u1=Array.from(new Set(arr1));
 console.log(u1);
 
    

and I want to get uniques only.Here is my attempt

learner
  • 65
  • 4
  • 2
    Because every object is unique, even if they have the same property values. See: `console.log({} === {})` is `false` which means that two empty objects are not equal. – Felix Kling May 10 '18 at 22:12
  • Yeah, `2 === 2`, `{} !== {}`, so they `Set` is doing just what it is supposed to do. – loganfsmyth May 10 '18 at 22:12

0 Answers0