Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js iterate set

const mySet = new Set( [ { a: 1 }, { b: 2 } ] );

// A) Using forEach()
mySet.forEach( item => console.log( item ) );

// B) Using for ... of
for (let item of mySet) {
  console.log(item);
}
// Output:
// { a: 1 }
// { b: 2 }
Comment

javascript Iterate Sets

const set = new Set([1, 2, 3]);

// looping through Set
for (let i of set) {
    console.log(i);
}
Comment

js iterating set

// 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

iterate set javascript

function logSetElements(value1, value2, set) {
  console.log(`s[${value1}] = ${value2}`);
}

new Set(['foo', 'bar', undefined]).forEach(logSetElements);

// expected output: "s[foo] = foo"
// expected output: "s[bar] = bar"
// expected output: "s[undefined] = undefined"
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs increase heap size 
Javascript :: status nodejs 
Javascript :: vue.js cdn 
Javascript :: angular lifecycle hooks 
Javascript :: convert number to array javascript 
Javascript :: useHistory goback 
Javascript :: iframe in angular 
Javascript :: Format number thousands k javascript 
Javascript :: how to concurrently run angular and node 
Javascript :: nazmul hassan 
Javascript :: how to get the nth child of dom js 
Javascript :: set an attribute background image javascript 
Javascript :: how to reset auto numeric js for input 
Javascript :: how to change the color using js 
Javascript :: javascript alphabet to number 
Javascript :: odd even condition with ternary operator 
Javascript :: radio button default checked react 
Javascript :: fs move file 
Javascript :: regex for 4 digit number javascript 
Javascript :: return longest string from array 
Javascript :: javascript object total 
Javascript :: javascript redirect 
Javascript :: redirect to homepage javascript 
Javascript :: replace node 
Javascript :: how to start react project 
Javascript :: js regex i modifier 
Javascript :: center an element react native 
Javascript :: give multiple classes in modular css react 
Javascript :: check if not checked vanila js 
Javascript :: check if variable is undefined or null jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =