I am looking to store a list of unique string (hence Set) and wanted to retrieve the value based on the index. I used get(index) But it turned out it returns undefined. So it seems I did not understand Set well.
In case value needed to be retrieved do we have to convert it back to the array and then only read it or with "get(index)" it can be achieved?
Also, I have checked Set tests to understand get(index) but still not clear.
const { Set } = require('immutable');
const set = Set(["ab", "cd", "ef"])
console.log(set.get(1)) //logs undefined
console.log(set.toJS()[1]) //logs "cd"
You can't index a Set like you can a List/Array. You can perform typical set operations - such as check if the Set contains an element by value, or remove an element by value. They are typically used for checking membership of data, not retrieving key/value pairs or values by index.
Is it possible to access the index inside a set? Is there a way to access the index of the set like so ? Show activity on this post. You can't index a Set like you can a List/Array. You can perform typical set operations - such as check if the Set contains an element by value, or remove an element by value.
The elements of the set will be stored in sorted order. There are several ways to access elements by index in a set some of them are: Set find c++: To access an element at the nth index, we must first construct an iterator pointing to the starting position and continue to increment the iterator until the nth element is reached.
How to get elements by index from HashSet in Java? 1 Using an array#N#We can convert the HashSet object to an array and then access the elements using the index as given... 2 Using an ArrayList or LinkedList#N#Instead of an array, we can also convert the HashSet object to an ArrayList or a... 3 Using an Iterator or a for loop More ...
Another way to do that would be to use Array.from(set)[0]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With