Maps and Sets
Set From Array
Remove duplicates, then keep the values that match a rule.
set-from-array
`Array.from(new Set(values))` is a compact way to remove duplicates while preserving first-seen order. After that, normal array methods can continue the pipeline.
Set From Array
set_from_array.js
Replay: real traced execution (multi-file project)
const minLength = 3;
const tags = Array.from(new Set(["js", "web", "js", "trace"]))
.filter((tag) => tag.length >= minLength)
.join(",");
console.log("minLength=" + minLength);
console.log("tags=" + tags);
const minLength = 2;
const tags = Array.from(new Set(["js", "web", "js", "trace"]))
.filter((tag) => tag.length >= minLength)
.join(",");
console.log("minLength=" + minLength);
console.log("tags=" + tags);
const minLength = 5;
const tags = Array.from(new Set(["js", "web", "js", "trace"]))
.filter((tag) => tag.length >= minLength)
.join(",");
console.log("minLength=" + minLength);
console.log("tags=" + tags);
minLength ← 3, tags ← web,trace
1const minLength→ 3 = 3; //@minLength=2, 52const tags→ web,trace = Array.from(new Set(["js", "web", "js", "trace"]))3 .filter((tag) => tag.length >= minLength)4 .join(",");56console.log("minLength=" + minLength3);7console.log("tags=" + tagsweb,trace);outputminLength=3 tags=web,trace
minLength ← 2, tags ← js,web,trace
1const minLength→ 2 = 2;2const tags→ js,web,trace = Array.from(new Set(["js", "web", "js", "trace"]))3 .filter((tag) => tag.length >= minLength)4 .join(",");56console.log("minLength=" + minLength2);7console.log("tags=" + tagsjs,web,trace);outputminLength=2 tags=js,web,trace
minLength ← 5, tags ← trace
1const minLength→ 5 = 5;2const tags→ trace = Array.from(new Set(["js", "web", "js", "trace"]))3 .filter((tag) => tag.length >= minLength)4 .join(",");56console.log("minLength=" + minLength5);7console.log("tags=" + tagstrace);outputminLength=5 tags=trace