-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
For Each HashMap Item #64
Comments
Hey there, yeah the docs are lacking at this point. There are a few ways you can iterate through entries: import * as HashMap from '@collectable/map';
const map = getMyHashMapSomehow();
// The HashMap structure actually implements [Symbol.iterator]:
for (let [key, value] of map) {
// ...
}
// The above internally just makes the following call:
const iterator = HashMap.entries(map);
const first = iterator.next();
const [key, value] = first.value;
// There are also methods for keys and values:
const keysIterator = HashMap.keys(map);
const valuesIterator = HashMap.values(map);
// And of course you can always make an array out of any of these:
const keys = Array.from(HashMap.keys(map));
const values = Array.from(HashMap.values(map));
const entries = Array.from(map); |
For a list of available functions and their signatures, check out: |
If I try to do:
I get this error:
Therefore the iteration won't work. |
Oh, that's TypeScript being lame. It is actually valid, but TypeScript is failing to recognise the |
I just ran the following quick tests in JavaScript and had no issues: // Test when constructing from an array
const map1 = HashMap.fromArray([
['foo', 10],
['bar', 20],
['baz', 30],
]);
for (let [key, value] of map1) {
console.log(key, value);
}
// Test when constructing from an object
const map2 = HashMap.fromObject({
foo: 10,
bar: 20,
baz: 30,
});
for (let [key, value] of map2) {
console.log(key, value);
}
// Test when adding items manually
let map3 = HashMap.empty();
map3 = HashMap.set('foo', 10, map3);
map3 = HashMap.set('bar', 20, map3);
map3 = HashMap.set('baz', 30, map3);
for (let [key, value] of map3) {
console.log(key, value);
}
// Test adding a lot of items
const map4 = HashMap.empty(true);
for (let i = 0; i < 1000; i++) {
HashMap.set(i, `#${i}`, map4);
}
for (let [key, value] of map4) {
console.log(key, value);
} |
Closing this as it appears to be a non-issue. |
I have a map of type: HashMapStructure<string, number>
How can I iterate through it?
Can't find any docs or intructions.
The text was updated successfully, but these errors were encountered: