To access items in a Set in TypeScript, you can use methods like forEach to iterate over the set or has to check for item existence.
In this example,
fruitSet
with some initial values.let fruitSet: Set<string> = new Set(['Apple', 'Banana', 'Cherry']);
fruitSet.forEach((item) => {
console.log(item);
});
Apple Banana Cherry
In this example,
numberSet
with some initial values.let numberSet: Set<number> = new Set([1, 2, 3, 4, 5]);
let containsThree: boolean = numberSet.has(3);
console.log('Set contains 3:', containsThree);
Set contains 3: true
In this tutorial, we learned How to Access Items in a Set in TypeScript language with well detailed examples.