get value from object javascript code example
Example 1: object values
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]
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] ]
Example 3: javascript get values from object
Object.values(data)
Example 4: get all entries in object as array hjs
const object1 = {
a: 'somestring',
b: 42
};
for (let [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed
Example 5: js get all values of object
Object.values(object1)
Example 6: javascript object get value by key
var obj = {
a: "A",
b: "B",
c: "C"
}
console.log(obj.a); // return string : A
var name = "a";
console.log(obj[name]);