0

Is it possible to define a 2 dimensional set in Javascript, which could look as follows:

let set = {}{};

and then to access this like a 2 dimensional array as set[][]; ?

By the way, an Array is not suitable for my purpose, because I need to index with strings instead of numbers.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
deppensido
  • 75
  • 5

1 Answers1

0

What you're describing doesn't seem like sets, you seem to be talking about an object whose values are other objects, e.g.

let set = {
    a: {x: 1, y: 2},
    b: {x: 10, y: 15}
}

You can then access this as set.a.x or set['b']['y']

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I have a combobox of several types as innerHTML and to each type belong several ids. So in that case each value should be a set of the corresponding ids. The types and corresponding ids appear dynamically. – deppensido Jun 11 '20 at 21:16
  • Don't put code in comments; and if you do, use backticks to delimit it, not [code]. – Barmar Jun 11 '20 at 21:23
  • the example you gave above is as similar as what I need. Is it possible to define it more general as: let set = { {}, {} } ? I just tried it, but doesn't work – deppensido Jun 11 '20 at 21:25
  • No, the elements of objects have to be `key: value` pairs. You can do `let set = {a: {}, b: {}}`. You can also just do `let set = {}` and you can add properties as needed like `set['a'] = {}` – Barmar Jun 11 '20 at 21:30
  • what I want to archieve is as follows: ids[type][id] = id; option.value = ids[type][id]; sorry, I have no idea, how to format code here – deppensido Jun 11 '20 at 21:30
  • It won't automatically create nested objects for you. You can do `ids[type] = ids[type] || {}; ids[type][id] = id;` – Barmar Jun 11 '20 at 21:31