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 :: mongoose return full object after inserting data to db 
Javascript :: ERROR in ./node_modules/pretty-format/node_modules/ansi-regex/index.js Module build failed: Error: ENOENT: no such file or directory 
Javascript :: crypto digest node.js 
Javascript :: redux store template 
Javascript :: js remove null object 
Javascript :: react developer cvs 
Javascript :: how to check if .each first element 
Javascript :: sharepoint javascript get last added item 
Javascript :: set prop as optional in react flow 
Javascript :: Google App Script Create Contact 
Javascript :: javascript hashmap equivalent 
Javascript :: table antd dosen t update 
Javascript :: submit file js 
Javascript :: one dimensional array in javascript 
Javascript :: python to javascript code converter 
Javascript :: What can I put in the parentheses of an if statement to make it false 
Javascript :: JS equal sibling btns height 
Javascript :: regex generator from text 
Javascript :: add link in react table 
Javascript :: date calendar show only icon click 
Javascript :: Function As Parameter In Self Invoking Function 
Javascript :: Save Function To Different Name 
Javascript :: check if a text field is empty javascript 
Javascript :: nodejs express parse query params boolean 
Javascript :: string inverter vs property binding in angular 
Javascript :: Error: Minified React error #321 
Javascript :: check if a specific user is banned discord js 
Javascript :: get a nodes path alias 
Javascript :: nextjs youtube embed 
Javascript :: Nested Components 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =