get object key from value javascript code example
Example 1: how to find the key of an value in an object
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
const map = {"first" : "1", "second" : "2"};
console.log(getKeyByValue(map,"2"));
Example 2: get keys objet javascript
var foo = {
'alpha': 'puffin',
'beta': 'beagle'
};
var keys = Object.keys(foo);
console.log(keys)
Example 3: javascript object get value by key
const person = {
name: 'Bob',
age: 47
}
Object.keys(person).forEach((key) => {
console.log(person[key]);
});
Example 4: object.keys
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
Example 5: javascript object get value by key
var obj = {
a: "A",
b: "B",
c: "C"
}
console.log(obj.a);
var name = "a";
console.log(obj[name]);