Skip to content

Instantly share code, notes, and snippets.

@liuwenzhuang
Created July 19, 2018 05:54
Show Gist options
  • Save liuwenzhuang/1407e3dedfdabf59cfed6d6bd96a3c6b to your computer and use it in GitHub Desktop.
Save liuwenzhuang/1407e3dedfdabf59cfed6d6bd96a3c6b to your computer and use it in GitHub Desktop.
使原生Object能够被遍历
/**
* 使object可遍历
* @param {Object} obj 需要处理的object
*/
function* objectEntries(obj) {
const propKeys = Reflect.ownKeys(obj);
for(const propKey of propKeys) {
yield [propKey, obj[propKey]];
}
}
const obj = {
a: 1,
b: '2',
};
for(const [key, value] of objectEntries(obj)) {
console.log(key, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment