打赏

ES2017 keys,values,entries使用

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]
}

Object.keys可以省略Object.

posted @ 2018-09-01 16:56  孟繁贵  阅读(595)  评论(0编辑  收藏  举报
TOP