Object.keys();是返回目标对象的一个key的数组,一般可用于对象、数组、字符串。
1.对象
var ad_info = { '1': 1, '2': 2, '3': 3 }; console.log(Object.keys(ad_info)); 结果:['1', '2', '3'] Object.keys(ad_info).map(function (key) { console.log(ad_info[key]);//加入一些数据操作 }); 结果:123
2.数组
var arr = ['人', '生', '自', '古', '谁', '无', '死', '留', '取', '丹', '心']; console.log(Object.keys(arr)); 结果:['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
3.字符串
var str = "人生自古谁无死留取丹心照汗青!"; console.log(Object.keys(str)); 结果:['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14']