🗺️ Mapy (obiekty)

Mapy to kolekcje par klucz-wartość. Podobne do obiektów w JavaScript lub słowników w Pythonie.

Tworzenie map

// Podstawowa składnia let person = { name: "Kitsune", age: 25, active: true }; // Pusta mapa let empty = {}; // Trailing comma dozwolona let obj = {a: 1, b: 2,}; // Computed property names let key = "dynamicKey"; let computed = { [key]: 42, ["prefix_" + key]: 100 }; print(computed["dynamicKey"]); // 42 // Property shorthand let x = 10, y = 20; let point = {x, y}; // {x: 10, y: 20}

Dostęp do właściwości

let obj = {name: "Kit", age: 25}; // Dot notation print(obj.name); // "Kit" // Bracket notation print(obj["age"]); // 25 // Dynamiczny klucz let key = "name"; print(obj[key]); // "Kit" // Optional chaining print(obj?.name); // "Kit" print(obj?.missing); // null (nie błąd)

Modyfikacja

let obj = {a: 1}; // Dodawanie/zmiana obj.b = 2; obj["c"] = 3; // Usuwanie obj.remove("a"); print(obj); // {b: 2, c: 3}

Właściwości map (OOP)

let m = {a: 1, b: 2, c: 3}; m.size; // 3 m.isEmpty; // false {}.isEmpty; // true

Metody map

let m = {a: 1, b: 2, c: 3}; // Pobieranie kluczy/wartości m.keys(); // ["a", "b", "c"] m.values(); // [1, 2, 3] m.entries(); // [["a", 1], ["b", 2], ["c", 3]] m.toList(); // [["a", 1], ["b", 2], ["c", 3]] // Sprawdzanie m.has("a"); // true m.has("z"); // false // Dostęp z domyślną wartością m.get("a"); // 1 m.getOrDefault("z", 99); // 99 // Modyfikacja m.set("d", 4); // Dodaj/zmień, zwraca mapę m.remove("a"); // Usuń, zwraca usuniętą wartość m.clear(); // Wyczyść wszystko

Higher-order methods

let prices = {apple: 2, banana: 5, cherry: 3}; // Filter prices.filter((k, v) => v > 2); // {banana: 5, cherry: 3} // Map prices.map((k, v) => v * 2); // {apple: 4, banana: 10, cherry: 6} // ForEach prices.forEach((k, v) => print(k, v)); // Reduce prices.reduce((acc, k, v) => acc + v, 0); // 10

Łączenie map

let m1 = {a: 1, b: 2}; let m2 = {c: 3, d: 4}; // Merge m1.merge(m2); // {a: 1, b: 2, c: 3, d: 4} // Spread operator let merged = {...m1, ...m2};

Dict Comprehension

let prices = {apple: 2, banana: 1, orange: 3}; // Transformacja let doubled = {k: v * 2 for k, v in prices}; // {apple: 4, banana: 2, orange: 6} // Z warunkiem let expensive = {k: v for k, v in prices if v > 1}; // {apple: 2, orange: 3}

Mapy z metodami

let counter = { value: 0, increment: fn() { this.value = this.value + 1; return this.value; }, reset: fn() { this.value = 0; } }; print(counter.increment()); // 1 print(counter.increment()); // 2 counter.reset(); print(counter.value); // 0

Chainowanie

let result = {a: 1, b: 2, c: 3, d: 4} .filter((k, v) => v > 1) .map((k, v) => v * 10) .values() .sum(); print(result); // 90 (20 + 30 + 40)

Destructuring

let config = { host: "localhost", port: 8080, debug: true }; // Destructuring z domyślnymi wartościami let {host, port, timeout = 5000} = config; print(host); // "localhost" print(port); // 8080 print(timeout); // 5000 (domyślna)

Iteracja

let obj = {x: 1, y: 2, z: 3}; // For-in zwraca entries [klucz, wartość] for (entry in obj) { print(entry[0] + " = " + entry[1]); } // Po kluczach for (key in obj.keys()) { print(key, obj[key]); }
📝 Uwaga o metodach vs właściwościach

Gdy mapa ma metodę o tej samej nazwie co wbudowana (np. .has()), wywołanie z nawiasami obj.has("x") woła metodę, a bez nawiasów obj.has pobiera właściwość.