JavaScript: 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 JavaScript: Sort Map ð