find val in object 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: javascript access property values list of objects
// Access all properties and values in a JS object:
let valuesArray = Object.entries(MyObject);
for (let value of valuesArray) {
document.write(value + "<br>"); // value is the property,value pair
}
let person = {
name: "Piet",
age: 42
};
Object.keys(person) // = ["name", "age"]
Object.values(person) // = ["Piet", 42]
Object.entries(person) // = [ ["name","Piet"], ["age",42] ]