Object.keys() - JavaScript | MDN 跳转到主要内容 跳转到搜索 MDN HTML HTML: Markup language HTML reference Elements Global attributes Attributes See all… HTML guides Responsive images HTML cheatsheet Date & time formats See all… Markup languages SVG MathML XML CSS CSS: Styling language CSS reference Properties Selectors At-rules Values See all… CSS guides Box model Animations Flexbox Colors See all… Layout cookbook Column layouts Centering an element Card component See all… JavaScriptJS JavaScript: Scripting language JS reference Standard built-in objects Expressions & operators Statements & declarations Functions See all… JS guides Control flow & error handing Loops and iteration Working with objects Using classes See all… Web APIs Web APIs: Programming interfaces Web API reference File system API Fetch API Geolocation API HTML DOM API Push API Service worker API See all… Web API guides Using the Web animation API Using the Fetch API Working with the History API Using the Web speech API Using web workers All All web technology Technologies Accessibility HTTP URI Web extensions WebAssembly WebDriver See all… Topics Media Performance Privacy Security Progressive web apps Learn Learn web development Frontend developer course Getting started modules Core modules MDN Curriculum Check out the video course from Scrimba, our partner Learn HTML Structuring content with HTML module Learn CSS CSS styling basics module CSS layout module Learn JavaScript Dynamic scripting with JavaScript module Tools Discover our tools Playground HTTP Observatory Border-image generator Border-radius generator Box-shadow generator Color format converter Color mixer Shape generator About Get to know MDN better About MDN Advertise with us Community MDN on GitHub Blog 切换侧边栏 面向开发者的 Web 技术 JavaScript JavaScript 参考 JavaScript 标准内置对象 Object Object.keys() 主题 跟随系统 浅色 深色 中文 (简体) 记住语言 了解更多 Deutsch English (US) Español Français 日本語 한국어 Português (do Brasil) Русский 中文 (简体) 正體中文 (繁體) 此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。 View in English Always switch to English Object.keys() 基线 广泛可用 自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。 了解更多 查看完整兼容性 Object.keys() 静态方法返回一个由给定对象自身的可枚举的字符串键属性名组成的数组。 本文内容 尝试一下 语法 描述 示例 规范 浏览器兼容性 参见 尝试一下 const object1 = { a: "somestring", b: 42, c: false, }; console.log(Object.keys(object1)); // Expected output: Array ["a", "b", "c"] 语法 js Object.keys(obj) 参数 obj 一个对象。 返回值 一个由给定对象自身可枚举的字符串键属性键组成的数组。 描述 Object.keys() 返回一个数组,其元素是字符串,对应于直接在对象上找到的可枚举的字符串键属性名。这与使用 for...in 循环迭代相同,只是 for...in 循环还会枚举原型链中的属性。Object.keys() 返回的数组顺序和与 for...in 循环提供的顺序相同。 如果你需要属性的值,请使用 Object.values()。如果你同时需要属性的键和值,请使用 Object.entries()。 示例 使用 Object.keys() js // 简单数组 const arr = ["a", "b", "c"]; console.log(Object.keys(arr)); // ['0', '1', '2'] // 类数组对象 const obj = { 0: "a", 1: "b", 2: "c" }; console.log(Object.keys(obj)); // ['0', '1', '2'] // 键的顺序随机的类数组对象 const anObj = { 100: "a", 2: "b", 7: "c" }; console.log(Object.keys(anObj)); // ['2', '7', '100'] // getFoo 是一个不可枚举的属性 const myObj = Object.create( {}, { getFoo: { value() { return this.foo; }, }, }, ); myObj.foo = 1; console.log(Object.keys(myObj)); // ['foo'] 如果你想要所有以字符串为键的自有属性,包括不可枚举的属性,参见 Object.getOwnPropertyNames()。 在基本类型中使用 Object.keys() 非对象参数会强制转换为对象。undefined 和 null 不能被强制转换为对象,会立即抛出 TypeError。只有字符串可以有自己的可枚举属性,而其他所有基本类型都返回一个空数组。 js // 字符串具有索引作为可枚举的自有属性 console.log(Object.keys("foo")); // ['0', '1', '2'] // 其他基本类型(除了 undefined 和 null)没有自有属性 console.log(Object.keys(100)); // [] 备注:在 ES5 中,将一个非对象传递给 Object.keys() 会抛出一个 TypeError。 规范 规范 ECMAScript® 2027 Language Specification # sec-object.keys 浏览器兼容性 启用 JavaScript 以查看此浏览器兼容性表。 参见 core-js 中 Object.keys 的 Polyfill 属性的可枚举性和所有权 Object.entries() Object.values() Object.prototype.propertyIsEnumerable() Object.create() Object.getOwnPropertyNames() Map.prototype.keys() 帮助改进 MDN 此页面对您有帮助吗? 是 否 了解如何参与贡献 此页面最后更新于 2025年10月23日,由 MDN 贡献者更新。 在 GitHub 上查看此页面 • 报告此内容的问题 筛选侧边栏 清除筛选输入 JavaScript 标准内置对象 Object 构造函数 Object() 构造函数 静态方法 Object.assign() Object.create() Object.defineProperties() Object.defineProperty() Object.entries() Object.freeze() Object.fromEntries() Object.getOwnPropertyDescriptor() Object.getOwnPropertyDescriptors() Object.getOwnPropertyNames() Object.getOwnPropertySymbols() getPrototypeOf() Object.groupBy() Object.hasOwn() is() Object.isExtensible() Object.isFrozen() Object.isSealed() Object.keys() Object.preventExtensions() Object.seal() Object.setPrototypeOf() Object.values() 实例方法 Object.prototype.__defineGetter__() Object.prototype.__defineSetter__() Object.prototype.__lookupGetter__() Object.prototype.__lookupSetter__() Object.prototype.hasOwnProperty() Object.prototype.isPrototypeOf() Object.prototype.propertyIsEnumerable() Object.prototype.toLocaleString() Object.prototype.toString() Object.prototype.valueOf() 实例属性 Object.prototype.__proto__ Object.prototype.constructor 继承 Object/Function 静态方法 Function.prototype.apply() Function.prototype.bind() Function.prototype.call() Function.prototype.toString() Function.prototype[Symbol.hasInstance]() 静态属性 Function.displayName Function:length Function.name Function.prototype.prototype Function.prototype.arguments Function.prototype.caller 实例方法 Object.prototype.__defineGetter__() Object.prototype.__defineSetter__() Object.prototype.__lookupGetter__() Object.prototype.__lookupSetter__() Object.prototype.hasOwnProperty() Object.prototype.isPrototypeOf() Object.prototype.propertyIsEnumerable() Object.prototype.toLocaleString() Object.prototype.toString() Object.prototype.valueOf() 实例属性 Object.prototype.__proto__ Object.prototype.constructor MDN 构建更美好互联网的蓝图。 MDN 关于 博客 Mozilla 招聘 与我们合作投放广告 MDN Plus 产品帮助 参与贡献 MDN 社区 社区资源 写作指南 MDN Discord MDN GitHub 主页 开发者 Web 技术 学习 Web 开发 指南 教程 术语表 Hacks 博客 Mozilla 网站隐私声明 遥测设置 法律声明 社区参与准则 本内容的部分版权归 mozilla.org 贡献者所有,©1998–2026。内容依据知识共享许可协议提供。