JavaScript sets and value objects [duplicate]
问题 This question already has answers here : user defined object equality for a set in harmony (es6) (2 answers) Closed 4 years ago . I want to create a set of value objects in JavaScript. The problem is that in JavaScript equality is based on identity. Hence, two different objects with the same value will be treated as unequal: var objects = new Set; objects.add({ a: 1 }); objects.add({ a: 1 }); alert(objects.size); // expected 1, actual 2 How do you work around this problem? 回答1: Use JSON