{"id":1091,"date":"2025-12-31T17:24:25","date_gmt":"2025-12-31T09:24:25","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1091"},"modified":"2025-12-31T17:24:25","modified_gmt":"2025-12-31T09:24:25","slug":"object-keys","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/31\/object-keys\/","title":{"rendered":"object.keys"},"content":{"rendered":"\n<p>\u4f60\u63d0\u5230\u7684 <strong><code>Object.keys<\/code><\/strong>\uff0c\u8fd9\u662f <strong>JavaScript \u5185\u7f6e\u5bf9\u8c61 <code>Object<\/code> \u7684\u4e00\u4e2a\u65b9\u6cd5<\/strong>\uff0c\u7528\u4e8e <strong>\u83b7\u53d6\u5bf9\u8c61\u81ea\u8eab\u53ef\u679a\u4e3e\u5c5e\u6027\u7684\u952e\u540d\u6570\u7ec4<\/strong>\u3002\u6211\u7ed9\u4f60\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u5305\u62ec\u7528\u6cd5\u3001\u793a\u4f8b\u548c\u6ce8\u610f\u70b9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u8bed\u6cd5<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.keys(obj)\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53c2\u6570 <code>obj<\/code><\/strong>\uff1a\u4e00\u4e2a\u666e\u901a\u5bf9\u8c61<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u503c<\/strong>\uff1a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u5bf9\u8c61\u81ea\u8eab\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\u540d\uff08\u5b57\u7b26\u4e32\u7c7b\u578b\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u57fa\u7840\u793a\u4f8b<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst person = {\n  name: &#039;Alice&#039;,\n  age: 25,\n  city: &#039;Beijing&#039;\n};\n\nconst keys = Object.keys(person);\nconsole.log(keys); \n\/\/ \u8f93\u51fa: &#x5B;&quot;name&quot;, &quot;age&quot;, &quot;city&quot;]\n\n<\/pre><\/div>\n\n\n<p>\u2705 <code>Object.keys<\/code> \u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684 <strong>\u5c5e\u6027\u540d\u6570\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528\u573a\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u904d\u5386\u5bf9\u8c61<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst obj = { a: 1, b: 2, c: 3 };\n\nObject.keys(obj).forEach(key =&gt; {\n  console.log(key, obj&#x5B;key]);\n});\n\n<\/pre><\/div>\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na 1\nb 2\nc 3\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 \u83b7\u53d6\u5bf9\u8c61\u5c5e\u6027\u6570\u91cf<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst obj = { x: 10, y: 20 };\nconsole.log(Object.keys(obj).length); \n\/\/ \u8f93\u51fa: 2\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 \u7b5b\u9009\u5bf9\u8c61\u5c5e\u6027<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst obj = { name: &#039;Tom&#039;, age: 30, city: &#039;Shanghai&#039; };\nconst filteredKeys = Object.keys(obj).filter(key =&gt; key !== &#039;age&#039;);\nconsole.log(filteredKeys); \n\/\/ \u8f93\u51fa: &#x5B;&quot;name&quot;, &quot;city&quot;]\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6ce8\u610f\u70b9<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u53ea\u83b7\u53d6\u81ea\u8eab\u5c5e\u6027<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst parent = { a: 1 };\nconst child = Object.create(parent);\nchild.b = 2;\n\nconsole.log(Object.keys(child)); \n\/\/ \u8f93\u51fa: &#x5B;&quot;b&quot;]\uff0c\u4e0d\u5305\u542b a\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u53ea\u83b7\u53d6\u53ef\u679a\u4e3e\u5c5e\u6027<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst obj = {};\nObject.defineProperty(obj, &#039;hidden&#039;, {\n  value: &#039;secret&#039;,\n  enumerable: false\n});\nobj.visible = &#039;ok&#039;;\n\nconsole.log(Object.keys(obj)); \n\/\/ \u8f93\u51fa: &#x5B;&quot;visible&quot;]\n\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u8fd4\u56de\u7684\u952e\u540d\u90fd\u662f\u5b57\u7b26\u4e32<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst obj = { 1: &#039;one&#039;, 2: &#039;two&#039; };\nconsole.log(Object.keys(obj)); \n\/\/ \u8f93\u51fa: &#x5B;&quot;1&quot;, &quot;2&quot;]\n\n<\/pre><\/div>\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u6570\u7ec4\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u7528<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst arr = &#x5B;&#039;a&#039;, &#039;b&#039;, &#039;c&#039;];\nconsole.log(Object.keys(arr)); \n\/\/ \u8f93\u51fa: &#x5B;&quot;0&quot;, &quot;1&quot;, &quot;2&quot;]\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u4e0e\u5176\u4ed6\u65b9\u6cd5\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8fd4\u56de\u5185\u5bb9<\/th><th>\u662f\u5426\u5305\u542b\u7ee7\u627f\u5c5e\u6027<\/th><th>\u662f\u5426\u5305\u542b\u4e0d\u53ef\u679a\u4e3e\u5c5e\u6027<\/th><\/tr><\/thead><tbody><tr><td><code>Object.keys(obj)<\/code><\/td><td>\u81ea\u8eab\u53ef\u679a\u4e3e\u5c5e\u6027\u540d\u6570\u7ec4<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><tr><td><code>Object.getOwnPropertyNames(obj)<\/code><\/td><td>\u81ea\u8eab\u5c5e\u6027\u540d\u6570\u7ec4<\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><tr><td><code>for...in<\/code><\/td><td>\u53ef\u679a\u4e3e\u5c5e\u6027\u540d\uff08\u81ea\u8eab+\u7ee7\u627f\uff09<\/td><td>\u2705<\/td><td>\u274c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><code>Object.keys(obj)<\/code> = \u83b7\u53d6\u5bf9\u8c61\u81ea\u8eab\u53ef\u679a\u4e3e\u5c5e\u6027\u540d\u6570\u7ec4<\/strong>\uff0c\u5e38\u7528\u4e8e\u904d\u5386\u3001\u7b5b\u9009\u548c\u7edf\u8ba1\u5bf9\u8c61\u5c5e\u6027\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u63d0\u5230\u7684 Object.keys\uff0c\u8fd9\u662f JavaScript \u5185\u7f6e\u5bf9\u8c61 Obje&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/31\/object-keys\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1091","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1091"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1091\/revisions"}],"predecessor-version":[{"id":1092,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1091\/revisions\/1092"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}