Created
July 19, 2018 05:54
-
-
Save liuwenzhuang/1407e3dedfdabf59cfed6d6bd96a3c6b to your computer and use it in GitHub Desktop.
使原生Object能够被遍历
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 使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