Suppose I have a function that receives either an object or an array. The goal is to iterate through each element and perform some action on each element along the way. While looping through an array can be achieved using forEach()
or a regular for loop, handling objects requires the usage of a for-in loop with an additional check using hasOwnProperty()
.
Is there a more universal approach that can handle both arrays and objects?
Here's my attempt at iterating through an object/array named 'value', but there seems to be no console messages displaying in case of objects:
keysArray = Object.keys(value);
for(let key in keysArray) {
console.log(value[key])
}