In this example, you will learn how to get object value by key in JavaScript.
JavaScript Object Get Value by Key Example
The following is an object in JavaScript:
var objct = { 1: "One", 2: "Two", 3: "Three", Four: 4, 5: "Five" }
Now use the following method to get the object value by key:
var b = "2"; console.log(objct[b]);
Output
Two
Another Example
var f = "Four"; console.log(objct[f]);
Output
4
Leave a comment