In the following JavaScript code,
obj = {};
// This work as intented
obj['a'] = { item1: 'a1', item2: 'a2' };
console.log(obj);
// Object.keys() works too
console.log(Object.keys(obj));
// forEach does not, why? and how to fix?
console.log('forEach');
obj.forEach(o => console.log(o));
What is needed to have forEach working?
forEachdoes not exist on objects. The duplicate question goes into great detail on how to loop across object properties, includingforEachacross object keys,for...in, and newer ES6 approaches.