get element by key javascript code example
Example 1: getkey by value js
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
Example 2: get keys of dictionary js
// Get keys of a dictionary
let dict = { a:1 , b:2 , c:3 }
console.log( Object.keys(dict) ) // expected output : ['a', 'b', 'c']
Example 3: javascript object get value by key
var obj = {
a: "A",
b: "B",
c: "C"
}
console.log(obj.a); // return string : A
var name = "a";
console.log(obj[name]);