0

I have the following objects:

var Something= {
    "None" : 0,
    "2D" : 1,
    "3D" : 2,
    "4D" : 3,
    "5D" : 4,
    "6D" : 5,
    "7D" : 6
};

function SomethingElse(obj) {

    this.2D = 0;
    this.3D = 0;
    this.4D = 0;
    this.5D = 0;
    this.6D = 0;
    this.7D = 0;

    for (var prop in obj) {
        this[prop] = obj[prop];
    }
}

I have an integer from which I want to get the propertyName of Something (for example, if I pass in 2 I should get back the string "3D". How do I do that?

Eventually I want to use that to set the value of SomethingElse[propertyName] = 0;

user247702
  • 23,641
  • 15
  • 110
  • 157
stackErr
  • 4,130
  • 3
  • 24
  • 48

1 Answers1

0

The straightforward solution would be to create a function that iterates with for (... in ...) through all properties of Something and returns the first matching property or else null.

function search(value)
{
    for (var prop in Something) {
        if (Something[prop] === value) {
            return prop;
        }
    }
    return null;
}
mpfefferle
  • 62
  • 1