1.Object.keys
var obj = { foo: "bar", baz: 42 };
Object.keys(obj)
// ["foo", "baz"]复制代码
2.Object.values
var obj = { 100: 'a', 2: 'b', 7: 'c' };
Object.values(obj)
// ["b", "c", "a"]复制代码
3.Object.entries
let obj = { a: 1, b: 2, c: 3 };
Object.entries(obj)
// [["a", 1], ["b", 2], ["c", 3]]复制代码
4.三种同时使用,简便写法
let {keys, values, entries} = Object;
let obj = { a: 1, b: 2, c: 3 };
for (let key of keys(obj)) {
console.log(key); // 'a', 'b', 'c'
}
for (let value of values(obj)) {
console.log(value); // 1, 2, 3
}
for (let [key, value] of entries(obj)) {
console.log([key, value]); // ['a', 1], ['b', 2], ['c', 3]
} 复制代码