import { BetterMap } from "https://deno.land/x/bettermap@v1.3.0/mod.ts";
Methods
Convert the map into an array of values / keys
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.array());
Return the nth element of the map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.at(-1)); // 28
Add all non-similar keys from another Map to this Map.
import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
type: "json",
};
interface PokemonData {
name: string;
id: number;
tier: string;
}
const b = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(0, 100)) {
b.set(`${pokemon.name}`, pokemon);
}
const c = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(50, 150)) {
c.set(`${pokemon.name}`, pokemon);
}
console.log(c.size)
c.combine(b)
console.log(c.size)
Array#every but for a Map
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.every(v => v > 10)); // false
console.log(map.every((_v, k) => k.startsWith("Dora"))); // true
Array#filter but for a Map
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.filter(v => v > 10));
console.log(map.filter((_v, k) => k.startsWith("Dora")))
Find an element from the Map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.find(v => v > 10)); // 28
Find a key from the Map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.findKey(v => v > 10)); // Dora
Get the first element(s) from the map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.first()); // 10
Get the first (n) element's key from the map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.firstKey()); // Doraemon
Convert the key-value pairs into key-value pairs... I mean a JavaScript object.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.json());
// {Doraemon: 10, Dora: 28}
Return the nth key of the map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.keyAt(-1)); // Dora
Get last value(s) in the Map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.last()); // 28
Get last key(s) in the Map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.lastKey()); // Dora
Map the Map into an Array.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.map(v => v - 10)); // [0, 18]
console.log(map.every((v, k) => k+v))); // ["Doraemon10", "Dora28"]
Get a random element from the BetterMap.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.random());
Get a random key from the BetterMap.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.randomKey());
Reduce data in the map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.reduce((acc, val) => acc + (val[1] > 10 ? "a" : "b") + val[0], ""));
// bDoraemonaDora
Check if at least one entry from the Map passes the function.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.some(v => v > 10)); // True
console.log(map.some(_v, k) => k.startsWith("Doras"))); // False
Sort elements in the better map.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
map.set("Pikachu", 7);
console.log(map.sort((v1, v2) => v1 - v2)); // Sorts in the order Pikachu -> Doraemon -> Dora.
Split the Map into two Maps using a filtering function.
const map = new BetterMap<string, number>("People");
map.set("Doraemon", 10);
map.set("Dora", 28);
console.log(map.split(v => v > 10));
// [{Doraemon => 28}, {Doraemon => 10}]
Static Methods
Create a new map from an existing Map or an array of key-value pairs.
import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
type: "json",
};
interface PokemonData {
name: string;
id: number;
tier: string;
}
const b = BetterMap.from(Pokemon.map(x => [x.name, x]))
console.log(b)
Return a Map with elements common to all maps supplied in the parameters.
import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
type: "json",
};
interface PokemonData {
name: string;
id: number;
tier: string;
}
const b = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(0, 100)) {
b.set(`${pokemon.name}`, pokemon);
}
const c = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(50, 150)) {
c.set(`${pokemon.name}`, pokemon);
}
console.log(c.size, b.size)
const a = BetterMap.intersect(c, b)
console.log(a.size)
Return the union of two maps. Maps are united by key and not by value.
import Pokemon from "https://deno.land/x/fortuna@v1.1.2/testdata/pokemon.json" assert {
type: "json",
};
interface PokemonData {
name: string;
id: number;
tier: string;
}
const b = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(0, 100)) {
b.set(`${pokemon.name}`, pokemon);
}
const c = new BetterMap<string, PokemonData>("Pokemon");
for (const pokemon of Pokemon.slice(50, 150)) {
c.set(`${pokemon.name}`, pokemon);
}
console.log(c.size, b.size)
const a = BetterMap.union(c, b)
console.log(a.size)