for in js object code example
Example 1: js for in object
var obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj));
for(const [i, val] of Object.entries(obj))
new_obj[i] = val;
Example 2: for in object javascript
var obj = { foo: 'bar', baz: 42 };
Object.keys(obj).forEach((key) => {
const el = obj[key];
console.log(
{
key: key,
value: el
}
);
});
Example 3: object for loop
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 4: forin js
let panier = ['fraise', 'banane', 'poire'];
for (const fruit in panier) {
console.log(panier[fruit]);
}