1. 程式人生 > >Object.keys(obj)

Object.keys(obj)

print div AR post var IT random pan reg

對象.keys

很明顯是獲得對象的key的一個數組

數組的key

arr = [‘a‘, ‘b‘, ‘c‘];
console.log(Object.keys(arr)); // console: [‘0‘, ‘1‘, ‘2‘]

對象的key

// array like object
var obj = { 0: ‘a‘, 1: ‘b‘, 2: ‘c‘ };
console.log(Object.keys(obj)); // console: [‘0‘, ‘1‘, ‘2‘]
// array like object with random key ordering
var an_obj = { 100: ‘a‘, 2: ‘b‘, 7: ‘c‘ };
console.log(Object.keys(an_obj)); // console: [‘2‘, ‘7‘, ‘100‘]

Object.keys(obj)