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.

5
  • Do you mean "can an object's values be objects?" Because in that case: yes. Commented Jun 11, 2020 at 20:42
  • I'm no mathematician but I don't know what "2 dimensional set" means. edit I guess "set of sets"? Commented Jun 11, 2020 at 20:42
  • Give an example of what you want. Commented Jun 11, 2020 at 20:43
  • Also note that JavaScript has a Set type. Commented Jun 11, 2020 at 20:43
  • 1
    Sets don't have indexes, so "index with strings instead of numbers" doesn't make sense. Commented Jun 11, 2020 at 20:44

1 Answer 1

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']

Sign up to request clarification or add additional context in comments.

6 Comments

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.
Don't put code in comments; and if you do, use backticks to delimit it, not [code].
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
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'] = {}
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.