Introduction to E2015 Set Objects

Introduction

Set objects are constructed using new Set().

Set object

A set is a “set of ” unique values. Say you have a simple game and need to track the position of mouse clicks. You’d store every position in a set object. Dupli…


This content originally appeared on DEV Community and was authored by Naftali Murgor

Introduction

Set objects are constructed using new Set().

Set object

A set is a “set of " unique values. Say you have a simple game and need to track the position of mouse clicks. You’d store every position in a set object. Duplicate values are discarded when an attempt to add the set object is made.

Sample code showing a simple Set object usage:

function main() {

    const gameScreen = document.getElementById('game-screen')
    gameScreen.addEventListener('click' updateAction)

    const cursorPositions = new Set()
    function updateAction(event) {
      let position = {x: e.clientX, y: e.clientY}
      cursorPositions.add(position) // any duplicate values are discarded, which is ideal in this case
    }
    // use unique cursorPositions below
}

while(true) {
  main()
}

Duplicate values are discarded when added to a set. This is useful for capturing and storing unique values where duplicate values are not needed.

const letters = new Set()
letters.add('A')
letters.add('B')
letters.add('A') // duplicate entry is ignored
console.log(letters) // Set {2} {'A', 'B'} 

Summary

  1. The set object provides a way of storing data where duplicate values are not required.

Note: Most languages including JavaScript offer a lot of language features but it’s not a good approach to try learning all these language features at a go. However, knowing they exist is enough as it helps one know where to look when the need arises.


This content originally appeared on DEV Community and was authored by Naftali Murgor


Print Share Comment Cite Upload Translate Updates
APA

Naftali Murgor | Sciencx (2022-01-25T20:30:51+00:00) Introduction to E2015 Set Objects. Retrieved from https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/

MLA
" » Introduction to E2015 Set Objects." Naftali Murgor | Sciencx - Tuesday January 25, 2022, https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/
HARVARD
Naftali Murgor | Sciencx Tuesday January 25, 2022 » Introduction to E2015 Set Objects., viewed ,<https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/>
VANCOUVER
Naftali Murgor | Sciencx - » Introduction to E2015 Set Objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/
CHICAGO
" » Introduction to E2015 Set Objects." Naftali Murgor | Sciencx - Accessed . https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/
IEEE
" » Introduction to E2015 Set Objects." Naftali Murgor | Sciencx [Online]. Available: https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/. [Accessed: ]
rf:citation
» Introduction to E2015 Set Objects | Naftali Murgor | Sciencx | https://www.scien.cx/2022/01/25/introduction-to-e2015-set-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.