{"id":3151,"date":"2024-11-10T16:31:52","date_gmt":"2024-11-10T08:31:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3151\/"},"modified":"2024-11-10T16:31:52","modified_gmt":"2024-11-10T08:31:52","slug":"js%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3151\/","title":{"rendered":"js\u5982\u4f55\u5224\u65ad\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u6570\u7ec4\u5224\u65ad\u65b9\u6cd5\u6709\u4e94\u79cd\uff1aarray.isarray() \u65b9\u6cd5\uff1binstanceof \u8fd0\u7b97\u7b26\uff1bobject.prototype.tostring.call() \u65b9\u6cd5\uff1b\u6570\u7ec4\u957f\u5ea6\uff1b\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\uff08\u4e0d\u63a8\u8350\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110805251957070.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5224\u65ad\u6570\u7ec4\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5224\u65ad\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u6570\u7ec4\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. Array.isArray() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8fd9\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>const myArray = [1, 2, 3];\nconsole.log(Array.isArray(myArray)); \/\/ true<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. instanceof \u8fd0\u7b97\u7b26\uff1a<\/strong><\/p>\n<p>\u8be5\u8fd0\u7b97\u7b26\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<pre>const myArray = [1, 2, 3];\nconsole.log(myArray instanceof Array); \/\/ true<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. Object.prototype.toString.call() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u63cf\u8ff0\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/p>\n<pre>const myArray = [1, 2, 3];\nconsole.log(Object.prototype.toString.call(myArray)); \/\/ \"[object Array]\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u6570\u7ec4\u957f\u5ea6\uff1a<\/strong><\/p>\n<p>\u6570\u7ec4\u6709\u4e00\u4e2a length \u5c5e\u6027\uff0c\u8868\u793a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002<\/p>\n<pre>const myArray = [1, 2, 3];\nconsole.log(myArray.length); \/\/ 3<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\uff1a<\/strong><\/p>\n<p>\u867d\u7136\u4e0d\u63a8\u8350\uff0c\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7\u9010\u4e2a\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u786e\u5b9a\u5b83\u662f\u5426\u4e3a\u6570\u7ec4\u3002<\/p>\n<pre>const myArray = [1, 2, 3];\nfor (let i = 0; i &lt; myArray.length; i++) {\n  if (typeof myArray[i] !== \"number\") {\n    console.log(\"\u4e0d\u662f\u6570\u7ec4\");\n  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5224\u65ad\u6570\u7ec4\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>\u6570\u7ec4\u5224\u65ad\u65b9\u6cd5\u6709\u4e94\u79cd\uff1aarray.isarray() \u65b9\u6cd5\uff1binstanceof \u8fd0\u7b97\u7b26\uff1bobject.prototype.tostring.call() \u65b9\u6cd5\uff1b\u6570\u7ec4\u957f\u5ea6\uff1b\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\uff08\u4e0d\u63a8\u8350\uff09\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad\u6570\u7ec4 \u5728 JavaScript \u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u6570\u7ec4\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. Array.isArray() \u65b9\u6cd5\uff1a \u8fd9\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002 const myArray = [1, 2, 3]; console.log(Array.isArray(myArray)); \/\/ true \u767b\u5f55\u540e\u590d\u5236 2. instanceof \u8fd0\u7b97\u7b26\uff1a \u8be5\u8fd0\u7b97\u7b26\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u3002 const myArray = [1, 2, 3]; console.log(myArray instanceof Array); \/\/ true \u767b\u5f55\u540e\u590d\u5236 3. Object.prototype.toString.call() \u65b9\u6cd5\uff1a \u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u63cf\u8ff0\u5bf9\u8c61\u7684\u7c7b\u578b\u3002 const myArray = [1, 2, 3]; console.log(Object.prototype.toString.call(myArray)); \/\/ &#8220;[object Array]&#8221; \u767b\u5f55\u540e\u590d\u5236 [&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-3151","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3151","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=3151"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3151\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}