0

Lets say we have:

  • 0: "APPLE"
  • 1: "ORANGE"
  • 2: "GRAPES"
  • ...

Is there a structure in JavaScript that allows me to get "ORANGE" from 1 and 1 from "ORANGE"? I remembered there was such a thing (not Object or Array) but can't remember its name, and I couldn't figure out a way to Google it without already knowing what it's called :(

Unless what I'm remembering is from ruby, not JS? In which case what do you call this?

EDIT

I am aware that Object and Array can achieve the same result. At this point I just really want to know if there's a specific structure for it, and what its name is, because I thought I remembered seeing it somewhere, either in JS or ruby. Please tell me what it's called, or tell me it doesn't exist, so I can have an ease of mind again..

For JS, I don't think it is Map either, because the value -> key direction still needed a bit of work.

Stefan
  • 109,145
  • 14
  • 143
  • 218
Lucia
  • 13,033
  • 6
  • 44
  • 50
  • 2
    maybe you are looking for [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) – Nina Scholz Nov 16 '18 at 10:10
  • You *can* easily do that with an object, though – CertainPerformance Nov 16 '18 at 10:14
  • @CertainPerformance Anyway getting key from value would require some loop, though – hindmost Nov 16 '18 at 10:17
  • 1
    Possible duplicate of [How can I create a two-way mapping in JavaScript, or some other way to swap out values?](https://stackoverflow.com/questions/21070836/how-can-i-create-a-two-way-mapping-in-javascript-or-some-other-way-to-swap-out) – Jeto Nov 16 '18 at 10:17
  • @hindmost It's trivial though, and so would a `Map`, right? – CertainPerformance Nov 16 '18 at 10:18
  • @CertainPerformance Agree. But AFAIU the OP wants a _single_ structure providing two ways of mapping, which neither structure in JS meet – hindmost Nov 16 '18 at 10:24
  • do you have a use case for this question? – Nina Scholz Nov 16 '18 at 10:33
  • @NinaScholz For unknown number of steps, I'm setting a `step_number:Number` to `step_name:String`, using the number value for conditions, string value for my own readability – Lucia Nov 16 '18 at 10:37
  • for this, you could use just an object with number -> string and string -> number relations. for maps, you need the same or something like this: https://stackoverflow.com/questions/47135661 – Nina Scholz Nov 16 '18 at 10:39

0 Answers0