get object key and 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: for key value in object javascript
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
Example 3: object keys javascript
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
Example 4: javascript object get value by key
const person = {
name: 'Bob',
age: 47
}
Object.keys(person).forEach((key) => {
console.log(person[key]);
});
Example 5: js find key by value in object
Object.keys(object).find(key => object[key] === value)
Example 6: 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]);