const name = { first:"hello", last:"world", fullname: "hello world" }
方式一、Object.keys
for (var i of Object.keys(name)) { console.log(i,"-",name[i]) }
方法二、Object.keys + Generator
function *map(item) { for (var i of Object.keys(item)) { yield [i,item[i]] } } for(var [key, value] of map(name)) { console.log(key,"-",value) }
注:generator函数中不能使用箭头函数
例:
*map = (item) => { for (var i of Object.keys(item)) { yield [i,item[i]] } }