JS: Truncate Map 🚀
Here's a function that return a new map, containing only first n entries.
/* return a new map, containing only first n entries. http://xahlee.info/js/js_map_truncate.html Version 2021-05-08 */ const xah_truncate_map = ((mapObj, n) => new Map([...mapObj.entries()].slice(0, Math.min(...[mapObj.size, n])))); // test // const m = new Map([[3, "n3"], [4, "n4"], [5, "n5"]]); // console.log(xah_truncate_map(m,2)); // Map { 3 => "n3", 4 => "n4" }
You probably want to use it with JS: Sort Map 🚀
JavaScript, Map Object
- JS: Map Object Tutorial
- JS: Difference, Object vs Map
- JS: Literal Expression for Map
- JS: Nested Map
- JS: Iterate Map Object
- JS: Convert Object to Map 🚀
- JS: Convert Map to Object 🚀
- JS: Map Equality 🚀
- JS: Map Filter 🚀
- JS: Swap Key Value of Map 🚀
- JS: Sort Map 🚀
- JS: Truncate Map 🚀
- JS: Map Substract 🚀
- JS: How Map Determines Uniqueness of Keys
- JS: Map Object
- JS: Map Constructor
- JS: Map.prototype