to get key and value from an object forEach code example
Example 1: javascript foreach key value
myObject ={a:1,b:2,c:3}
//es6
Object.entries(myObject).forEach(([key, value]) => {
console.log(key , value); // key ,value
});
//es7
Object.keys(myObject).forEach(key => {
console.log(key , myObject[key]) // key , value
})
Example 2: foreach object javascript
const obj = {
a: "aa",
b: "bb",
c: "cc",
};
//This for loop will loop through all keys in the object.
// You can get the value by calling the key on the object with "[]"
for(let key in obj) {
console.log(key);
console.log(obj[key]);
}
//This will return the following:
// a
// aa
// b
// bb
// c
// cc