{"id":3378,"date":"2024-11-10T14:21:10","date_gmt":"2024-11-10T06:21:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3378\/"},"modified":"2024-11-10T14:21:10","modified_gmt":"2024-11-10T06:21:10","slug":"js%e4%b8%ad%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%89%80%e6%9c%89%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3378\/","title":{"rendered":"js\u4e2d\u5982\u4f55\u83b7\u53d6\u6240\u6709\u7684\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u83b7\u53d6\u6240\u6709\u503c\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\uff1a\u4f7f\u7528 foreach \u5faa\u73af\u904d\u5386\u5143\u7d20\u5e76\u6253\u5370\u4f7f\u7528 map \u5faa\u73af\u521b\u5efa\u65b0\u6570\u7ec4\u5e76\u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u5bf9\u8c61\uff1a\u4f7f\u7528 for&#8230;in \u5faa\u73af\u904d\u5386\u952e\u548c\u503c\u4f7f\u7528 object.values() \u65b9\u6cd5\u83b7\u53d6\u503c\u5217\u8868\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\uff1a\u4f7f\u7528 charat() \u6216 split() \u65b9\u6cd5\u6570\u5b57\uff1a\u4f7f\u7528 tostring() \u65b9\u6cd5\u5e03\u5c14\u503c\uff1a\u4f7f\u7528 valueof() \u65b9\u6cd5\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110811092268127.jpg\" class=\"aligncenter\" title=\"js\u4e2d\u5982\u4f55\u83b7\u53d6\u6240\u6709\u7684\u503c\u63d2\u56fe\" alt=\"js\u4e2d\u5982\u4f55\u83b7\u53d6\u6240\u6709\u7684\u503c\u63d2\u56fe\" \/><\/p>\n<h2>JavaScript \u4e2d\u5982\u4f55\u83b7\u53d6\u6240\u6709\u503c<\/h2>\n<p>\u5728 JavaScript \u4e2d\uff0c\u83b7\u53d6\u6240\u6709\u503c\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7ed3\u6784\u548c\u83b7\u53d6\u503c\u7684\u5177\u4f53\u76ee\u7684\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u83b7\u53d6\u6240\u6709\u503c\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3>\u6570\u7ec4<\/h3>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 forEach \u5faa\u73af<\/strong><\/p>\n<pre>const myArray = [1, 2, 3, 4, 5];\n\nmyArray.forEach((element) =&gt; {\n  console.log(element);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>1\n2\n3\n4\n5<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 map \u5faa\u73af<\/strong><\/p>\n<pre>const newArr = myArray.map((element) =&gt; {\n  return element * 2;\n});\n\nconsole.log(newArr);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[ 2, 4, 6, 8, 10 ]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u5bf9\u8c61<\/h3>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 for&#8230;in \u5faa\u73af<\/strong><\/p>\n<pre>const myObject = {\n  name: 'John Doe',\n  age: 25,\n  city: 'New York'\n};\n\nfor (const key in myObject) {\n  console.log(key, myObject[key]);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>name John Doe\nage 25\ncity New York<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 Object.values() \u65b9\u6cd5<\/strong><\/p>\n<pre>const values = Object.values(myObject);\nconsole.log(values);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[ 'John Doe', 25, 'New York' ]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u5176\u4ed6\u6570\u636e\u7c7b\u578b<\/h3>\n<p>\u5bf9\u4e8e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5e03\u5c14\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>\u5b57\u7b26\u4e32\uff1a<\/strong>\u4f7f\u7528 charAt() \u6216 split() \u65b9\u6cd5<\/li>\n<li> <strong>\u6570\u5b57\uff1a<\/strong>\u4f7f\u7528 toString() \u65b9\u6cd5<\/li>\n<li> <strong>\u5e03\u5c14\u503c\uff1a<\/strong>\u4f7f\u7528 valueOf() \u65b9\u6cd5<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u4e2d\u5982\u4f55\u83b7\u53d6\u6240\u6709\u7684\u503c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u83b7\u53d6\u6240\u6709\u503c\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\uff1a\u4f7f\u7528 foreach \u5faa\u73af\u904d\u5386\u5143\u7d20\u5e76\u6253\u5370\u4f7f\u7528 map \u5faa\u73af\u521b\u5efa\u65b0\u6570\u7ec4\u5e76\u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u5bf9\u8c61\uff1a\u4f7f\u7528 for&#8230;in \u5faa\u73af\u904d\u5386\u952e\u548c\u503c\u4f7f\u7528 object.values() \u65b9\u6cd5\u83b7\u53d6\u503c\u5217\u8868\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\uff1a\u4f7f\u7528 charat() \u6216 split() \u65b9\u6cd5\u6570\u5b57\uff1a\u4f7f\u7528 tostring() \u65b9\u6cd5\u5e03\u5c14\u503c\uff1a\u4f7f\u7528 valueof() \u65b9\u6cd5 JavaScript \u4e2d\u5982\u4f55\u83b7\u53d6\u6240\u6709\u503c \u5728 JavaScript \u4e2d\uff0c\u83b7\u53d6\u6240\u6709\u503c\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7ed3\u6784\u548c\u83b7\u53d6\u503c\u7684\u5177\u4f53\u76ee\u7684\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u83b7\u53d6\u6240\u6709\u503c\u7684\u65b9\u6cd5\uff1a \u6570\u7ec4 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 forEach \u5faa\u73af const myArray = [1, 2, 3, 4, 5]; myArray.forEach((element) =&gt; { console.log(element); }); \u767b\u5f55\u540e\u590d\u5236 \u8f93\u51fa\uff1a 1 2 3 4 5 \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 map \u5faa\u73af const [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3378","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3378"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3378\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}