get keys and values from object javascript code example
Example 1: javascript foreach object keys
var lunch = {
sandwich: 'ham',
snack: 'chips',
drink: 'soda',
desert: 'cookie',
guests: 3,
alcohol: false,
};
Object.keys(lunch).forEach(function (item) {
console.log(item);
console.log(lunch[item]);
});
Example 2: get keys objet javascript
var foo = {
'alpha': 'puffin',
'beta': 'beagle'
};
var keys = Object.keys(foo);
console.log(keys)
Example 3: Return the Objects Keys and Values
const keysAndValues = (obj) => [Object.keys(obj), Object.values(obj)];
keysAndValues({a: 1, b: 2, c: 3});
keysAndValues({a: "Dell", b: "Microsoft", c: "Google"}));
keysAndValues({key1: true, key2: false, key3: undefined});
Example 4: js get object keys
myObject = {
"key": "value"
}
Object.keys(myObject);
Example 5: javascript object get value by key
const person = {
name: 'Bob',
age: 47
}
Object.keys(person).forEach((key) => {
console.log(person[key]);
});
Example 6: how to get keys in an object javascript
Object.keys(whateverYourObjectIsCalled)