Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Iterating set object javascript

// iterate over items in set
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet1) console.log(item)

// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet1.keys()) console.log(item)

// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet1.values()) console.log(item)

// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
// (key and value are the same here)
for (let [key, value] of mySet1.entries()) console.log(key)

// convert Set object to an Array object, with Array.from
const myArr = Array.from(mySet1) // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}]

// the following will also work if run in an HTML document
mySet1.add(document.body)
mySet1.has(document.querySelector('body')) // true

// converting between Set and Array
const mySet2 = new Set([1, 2, 3, 4])
mySet2.size                    // 4
[...mySet2]                    // [1, 2, 3, 4]

// intersect can be simulated via
const intersection = new Set([...mySet1].filter(x => mySet2.has(x)))

// difference can be simulated via
const difference = new Set([...mySet1].filter(x => !mySet2.has(x)))

// Iterate set entries with forEach()
mySet2.forEach(function(value) {
  console.log(value)
})

// 1
// 2
// 3
// 4
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript debugger 
Javascript :: js parameter vs argument 
Javascript :: javascript arrow functions to create methods inside objects 
Javascript :: angular material button color 
Javascript :: textbox value length in javascript 
Javascript :: angular mouseenter 
Javascript :: vue add watcher 
Javascript :: add marker on map geocoder result mapbox 
Javascript :: asynchronous function using function constructor 
Javascript :: multi filtering react 
Javascript :: react hero slider 
Javascript :: basics of switch case and if else 
Javascript :: keep value after refresh javascript 
Javascript :: pass props in react 
Javascript :: how to check for null in javascript 
Javascript :: jquery send to another page 
Javascript :: how to decode jwt token in angular 
Javascript :: javascript custom modal 
Javascript :: custom event example 
Javascript :: Generate a random Id safely 
Javascript :: 2 dimensional array index of element value 
Javascript :: Example of Reactjs Controlled-Components 
Javascript :: diferença entre let e var 
Javascript :: how to get form value 
Javascript :: Using redux on react extension 
Javascript :: if condition javascript 
Javascript :: javascript bind multiple arguments 
Javascript :: aws lambda send json 
Javascript :: js print objects 
Javascript :: polymorphism javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =