Skip to content

Instantly share code, notes, and snippets.

@Ugarz
Created March 13, 2018 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ugarz/3e80359b378065314405f070ece3bb54 to your computer and use it in GitHub Desktop.
Save Ugarz/3e80359b378065314405f070ece3bb54 to your computer and use it in GitHub Desktop.
Use Sets in Javascript

Sets in Javascript

const storePersons = new Set();

const personn1 = {
    "user": 4321,
    "type": "female"
}
const personn2 = {
    "user": 1234,
    "type": "male"
}

storePersons.add(personn1);
storePersons.add(personn2);

function has(value) {
    console.log(`Does the store contains ${JSON.stringify(value)} ?`, storePersons.has(value))
}

console.log('Final Set :', storePersons)
console.log('---')

has('hey')
has(1234)
has(4321)
has({ user: 4321 })
has({ user: 1234 })

console.log('---')

// Final Set : Set { { user: 4321, type: 'female' }, { user: 1234, type: 'male' } }
// ---
// Does the store contains "hey" ? false
// Does the store contains 1234 ? false
// Does the store contains 4321 ? false
// Does the store contains {"user":4321} ? false
// Does the store contains {"user":1234} ? false
// ---

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment