Skip to content

Instantly share code, notes, and snippets.

@chathurawidanage
Last active November 20, 2019 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chathurawidanage/c07d2c7209c097b8f69ee61773569e7c to your computer and use it in GitHub Desktop.
Save chathurawidanage/c07d2c7209c097b8f69ee61773569e7c to your computer and use it in GitHub Desktop.
Iterating Over Object Entries in JS using Object.getOwnPropertyNames | Performance Comparison [https://gists.cwidanage.com/2018/06/how-to-iterate-over-object-entries-in.html]
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
// convinient forEach
Object.getOwnPropertyNames(obj).forEach(key => {
let value = obj[key];
//use key and value here
});
// traditional for loop
let props = Object.getOwnPropertyNames(obj);
for(let i = 0; i<props.length;i++){
let value = obj[props[i]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment