get key value of object 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 of dictionary js
let dict = { a:1 , b:2 , c:3 }
console.log( Object.keys(dict) )
Example 3: for key value in object javascript
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
Example 4: get all keys of object in javascript
myObject = {
"key": "value",
"key2":"value2"
}
Object.keys(myObject);
Example 5: get keys of object js
var buttons = {
foo: 'bar',
fiz: 'buz'
};
for ( var property in buttons ) {
console.log( property );
}
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]);