Let's say I have a function that receives either an object or an array. I would like to iterate through each element, and do something to each element as I go. I know that I can iterate through an array with forEach()
or a normal for loop. I can also iterate through an object and array with a for in loop. But unlike the array case I have to check the object's elements with hasOwnProperty()
.
Can I just apply some general piece of code to both arrays and objects?
Update. This is my attempt for some object/array called value, but no console messages appear for the object case:
keysArray = Object.keys(value);
for(let key in keysArray) {
console.log(value[key])
}