1

I have an object listing keyCodes:

var keyCodes = {
    "a": 97, "b": 98, "c": 99, "d": 100, "e": 101, "f": 102,
    "g": 103, "h": 104, "i": 105, "j": 106, "k": 107, "l": 108,
    "m": 109, "n": 110, "o": 111, "p": 112, "q": 113, "r": 114,
    "s": 115, "t": 116, "u": 117, "v": 118, "w": 119, "x": 120,
    "y": 121, "z": 122
};

But I need to reference the value, not the attribute. How efficiently can I do this without rewriting the whole object?

5
  • Where you want to reference the values ? Commented Feb 22, 2017 at 4:28
  • 1
    keyCodes is an Object, not an Array Commented Feb 22, 2017 at 4:28
  • Do you mean to swap the key and value in an object? Commented Feb 22, 2017 at 4:29
  • You just want to reference the value only for few of the keyCodes or all of them? Commented Feb 22, 2017 at 4:33
  • Let me explain. I want to get the attribute of the object according to the value. In this case, the value is keyCode. So, i want to search the object to find the correct attribute for the value. Commented Feb 23, 2017 at 4:30

3 Answers 3

1

Generate a new object where property names are the value and values are the property name where use Array#reduce and Object.keys method to generate the new object.

var keyCodes = {
    "a": 97, "b": 98, "c": 99, "d": 100, "e": 101, "f": 102,
    "g": 103, "h": 104, "i": 105, "j": 106, "k": 107, "l": 108,
    "m": 109, "n": 110, "o": 111, "p": 112, "q": 113, "r": 114,
    "s": 115, "t": 116, "u": 117, "v": 118, "w": 119, "x": 120,
    "y": 121, "z": 122
  };

var res = Object.keys(keyCodes).reduce(function(obj, k) {
  obj[keyCodes[k]] = k;
  return obj;
}, {})

console.log(res[121])
console.log(res[106])
console.log(res[110])

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

2 Comments

OP's post: How efficiently can I do this without rewriting the whole object?
@ryanlutgen I find that statement from the OP to be confusing. Perhaps they just don't want to hard-code another object.
1

You can get array of values using Object.values

var values = Object.values(keyCodes);
console.log(values[0]);

And if you want a single value you can do this

var a = keyCodes["a"];
console.log(a);

If you want to get single property name from value you can try this

var val = 110; 
var propName;
for (var prop in keyCodes) {
if (keyCodes[prop] == val) {
   propName = prop;
   break;
  }
}
console.log(propName);

Comments

0

Might be a bit silly, but i'm assuming you want to get the key based on the number value? (over the keyCodes), probably want to pass in multiple values in the func, typecheck the values (between 97-122)

idk tho;

var keyCodes = {
  "a": 97, "b": 98, "c": 99, "d": 100, "e": 101, "f": 102,
  "g": 103, "h": 104, "i": 105, "j": 106, "k": 107, "l": 108,
  "m": 109, "n": 110, "o": 111, "p": 112, "q": 113, "r": 114,
  "s": 115, "t": 116, "u": 117, "v": 118, "w": 119, "x": 120,
  "y": 121, "z": 122
};

var getkey = function(value) {
  for (key in keyCodes) { 
    if (keyCodes[key] == value) return key;
  }
}

getkey(122) // returns 'z';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.