Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get index based value from Set

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"
like image 821
Anil Namde Avatar asked Jun 23 '17 07:06

Anil Namde


People also ask

How to index a set in Java?

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

How to access elements by Index in a set in C++?

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?

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


1 Answers

Another way to do that would be to use Array.from(set)[0].

like image 177
user1514042 Avatar answered Sep 27 '22 15:09

user1514042