Quantcast
Channel: Iterate through object properties - Stack Overflow
Viewing all articles
Browse latest Browse all 35

Answer by NoWhere Man for Iterate through object properties

0
0

Simple and clear way to reach this, im modern JS without iterate the prototypes, is like this:

Object.prototype.iterateProperties = ((callback) => {   Object.keys(obj).filter(key => obj.hasOwnProperty(key)).forEach((key) => {      callback(key, obj[key]);   });});

Explain

This code creates a function in the prototype of all object - means function that is accessible in every Object instance.The function iterate all own properties of the object and run a callback function which gets (key, value) for each propery in the object.

Example for use

obj.iterateProperties((key, value) => console.log(key +': '+ value));

Viewing all articles
Browse latest Browse all 35

Latest Images

Trending Articles



Latest Images