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

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 :: js export options 
Javascript :: react hook form example stack overflow 
Javascript :: eslint disable line 
Javascript :: [Object: null prototype] appolo 
Javascript :: How to change height of bottom material tab navigator from react-naviagtion 
Javascript :: is value in list javascript 
Javascript :: temporal dead zone in es6 
Javascript :: javascript find missing number 
Javascript :: find element in an array and replace it by a callback function 
Javascript :: js animations 
Javascript :: turn off js console 
Javascript :: vscode regex replace 
Javascript :: upload photos cypress 
Javascript :: regexes 
Javascript :: cypress set date to specific date 
Javascript :: GET FORM VALUE 
Javascript :: case switch javascript 
Javascript :: remove in javascript 
Javascript :: prototype javascript 
Javascript :: create record mongoose model 
Javascript :: elif in js 
Javascript :: has class in jquery 
Javascript :: map and filter js 
Javascript :: sequilze REACTJS 
Javascript :: label tag alternative in react native 
Javascript :: add to a js object 
Javascript :: axios 400 bad request 
Javascript :: how to set variable in discord.js 
Javascript :: hashnode 
Javascript :: react native login screen 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =