{"id":224,"date":"2023-02-23T06:34:14","date_gmt":"2023-02-22T22:34:14","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=224"},"modified":"2023-04-30T15:23:43","modified_gmt":"2023-04-30T07:23:43","slug":"js-traverses-objects-maps-and-arrays","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/23\/js-traverses-objects-maps-and-arrays\/","title":{"rendered":"JS\u904d\u5386\u5bf9\u8c61\u3001map\u53ca\u6570\u7ec4"},"content":{"rendered":"<h2>\u904d\u5386\u5bf9\u8c61<\/h2>\n<h3>for in<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">const user = {\n  id: 1,\n  name: &#039;Joe.Ye&#039;,\n  age: 18\n}\n\nfor(let key in user) {\n  console.log(key + &#039;: &#039; + user[key])\n}<\/code><\/pre>\n<h3>Object.keys<\/h3>\n<ul>\n<li>Object.keys(obj)<\/li>\n<li>Object.values(obj)<\/li>\n<\/ul>\n<p>\u8fd4\u56deobj\u5bf9\u8c61\u7684key\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u53cavalue\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u6570\u7ec4<\/p>\n<pre><code class=\"language-javascript\">const user = {\n  id: 1,\n  name: &#039;Joe.Ye&#039;,\n  age: 18\n}\n\nconsole.log(Object.keys(user))\nconsole.log(Object.values(user))<\/code><\/pre>\n<h3>Object.getOwnPropertyNames(obj)<\/h3>\n<p>\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u5bf9\u8c61\u81ea\u8eab\u7684\u6240\u6709\u5c5e\u6027\uff08\u5305\u542b\u4e0d\u53ef\u679a\u4e3e\u5c5e\u6027\uff09<\/p>\n<pre><code class=\"language-javascript\">const user = {\n  id: 1,\n  name: &#039;Joe.Ye&#039;,\n  age: 18\n}\n\nObject.getOwnPropertyNames(user).forEach(function(key){\n  console.log(key + &#039;: &#039; + user[key])\n})<\/code><\/pre>\n<h2>\u904d\u5386map<\/h2>\n<h3>for in<\/h3>\n<pre><code class=\"language-javascript\">var map = {\n  &quot;name&quot;: &quot;Joe.Ye&quot;,\n  &quot;homepage&quot;: &quot;http:\/\/www.appblog.cn&quot;\n};\n\nfor (var key in map) {\n  console.log(&quot;map[&quot; + key + &quot;] = &quot; + map[key]);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u904d\u5386\u5bf9\u8c61 for in const user = { id: 1, name: &#039;Joe.Ye&#038;#0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[84],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}