Object.keys():遍历对象的所有属性,该方法返回一个数组,数组内容就是对象的所有键名。
传入对象,返回属性名
var obj= {'a':'meng','b':'lin'}
console.log(Object.keys(obj));
// ['a','b']
var obj={ 100: "a", 2: "b", 7: "c"};
console.log(Object.keys(obj1));
// console: ["2", "7", "100"]
var obj2 = Object.create({}, { getFoo : { value : function () { return this.foo } } });
obj2.foo = 1;
console.log(Object.keys(obj2));
// console: ["foo"]
传入字符串,返回索引
var str = 'ab1234';
console.log(Object.keys(str)); //[0,1,2,3,4,5]
获取字符串中的某一个值
var str = 'ab1234';
console.log(Object.keys(str)); //[0,1,2,3,4,5]
Object.keys(str).map(function (index) {
console.log(str[index]);
});
//a,b,1,2,3,4
数组 返回索引
var arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // console: ["0", "1", "2"]