get key by 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: getkey by value js

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}

Example 3: javascript object get value by key

const person = {
  name: 'Bob',
  age: 47
}

Object.keys(person).forEach((key) => {
  console.log(person[key]); // 'Bob', 47
});

Example 4: js find key by value in object

Object.keys(object).find(key => object[key] === value)

Example 5: js object keys

var myObj = {no:'u',my:'sql'}
var keys = Object.keys(myObj);//returnes the array ['no','my'];

Example 6: get all keys of object in javascript

myObject = {
	"key": "value",
    "key2":"value2"
}
Object.keys(myObject);
//console.log(Object.keys(myObject))   = ["key", "key2"]