Skip to content

JavaScript set entries() Method

  • by

JavaScript set entries() Method used to return a new Iterator object that contains an array of key-value pairs for each element in the Set object, in insertion order.

set.entries()

Note: The set does not contain key-value pairs, to keep similarities with the map entries() method, it will return key-value pairs with the same value.

JavaScript set entries() examples

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script >
    const mySet = new Set(["apple", "banana", "orange"]);

    const setIterator = mySet.entries();

    for (const entry of setIterator) {
      console.log(entry);
    }

  </script>
</body>
</html>

Output:

JavaScript set entries() Method

You can also use the spread operator (...) to convert the iterator to an array of key-value pairs:

const mySet = new Set(["apple", "banana", "orange"]);

const entriesArray = [...mySet.entries()];

console.log(entriesArray);

Do comment if you have any doubts or suggestions on this Js set method topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *