Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JavaScript equivalent to java LinkedHashSet?

I have a list of lists containing IDs in javascript, I want them to be added to a set which maintains insertion order and has only unique values

like image 728
Ramanan Durairaj Avatar asked Jul 23 '26 11:07

Ramanan Durairaj


1 Answers

This is exactly what JavaScript's Set does. It contains only unique values, and iterates in insertion order. Example:

const s = new Set();
s.add("q");
s.add("x");
s.add("c");
s.add("q"); // duplicate
for (const v of s) {
  console.log(v); // q, x, c -- insertion order
}
like image 115
T.J. Crowder Avatar answered Jul 25 '26 02:07

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!