Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The `.key` is coming out as undefined. What do I do to make it come out as an actual key?

This comes out as undefined. Why is this?

var possibleKeyCodes = [ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ];

console.log(possibleKeyCodes[0].key);

I expected it to give me a key name as a string. It just gave me an undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

var possibleKeyCodes = [ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ];

console.log(possibleKeyCodes[0].key);

Your array is an array of numbers. You are trying to treat an element of this array as it is an object with key property

If you need the ascii repreentation of that number you can do it like that

console.log(String.fromCharCode(possibleKeyCodes[0]))

Or as a snippet

var possibleKeyCodes = [ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ];
console.log(String.fromCharCode(possibleKeyCodes[0]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading