js 中Object.keys()返回string类型的索引

Object.keys(obj)

参数 obj:要返回其枚举自身属性的对象

返回值:一个表示给定对象的所有可枚举属性的字符串数组

 如果是key-value的形式,会根据key来排序

eg-

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

// 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 anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']

// getFoo is a property which isn't enumerable
var myObj = Object.create({}, {
  getFoo: {
    value: function () { return this.foo; }
  } 
});
myObj.foo = 1;
console.log(Object.keys(myObj)); // console: ['foo']
posted @ 2020-09-10 11:43  带着bug上线  阅读(881)  评论(0编辑  收藏  举报