Reputation: 1548
I have a map in react with key and value both as string,like below:
let map=new Map();
map.set("foo","bar");
map.set("foo-bar","bar-foo");
How can i fetch the first value from this map ?
Upvotes: 0
Views: 1515
Reputation: 12737
You can use map
built in keys()
method that returns an iterator.
Then you can call .next()
on the iterator to fetch the first item.
The item is an object, so you will need to use the property value
.
let map=new Map();
map.set("foo","bar");
map.set("foo-bar","bar-foo");
const keysIterator = map.keys();
const firstKeyObject = keysIterator.next();
const firstKey = firstKeyObject.value;
console.log(map.get(firstKey));
// or simply:
console.log(map.get( map.keys().next().value ));
Upvotes: 2