{"id":6486,"date":"2023-01-08T21:15:20","date_gmt":"2023-01-08T13:15:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6486"},"modified":"2023-01-08T21:15:20","modified_gmt":"2023-01-08T13:15:20","slug":"%e4%ba%86%e8%a7%a3reactlist%e7%9a%84%e4%ba%a4%e9%9b%86intersection%e5%8a%9f%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e4%ba%86%e8%a7%a3reactlist%e7%9a%84%e4%ba%a4%e9%9b%86intersection%e5%8a%9f%e8%83%bd-2\/","title":{"rendered":"\u4e86\u89e3ReactList\u7684\u4ea4\u96c6(Intersection)\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, List, Intersection\"><\/p>\n<h1>\u4f7f\u7528 React.JS \u4f86\u8655\u7406 List \u7684\u4ea4\u96c6(intersection)<\/h1>\n<p>React.JS \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u51fd\u5f0f\u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u3002\u5728 React.JS \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 List \u7684\u4ea4\u96c6(intersection)\u4f86\u8655\u7406\u8cc7\u6599\u3002<\/p>\n<p>List \u7684\u4ea4\u96c6(intersection)\u662f\u6307\u5169\u500b\u6216\u591a\u500b List \u4e2d\u5171\u6709\u7684\u5143\u7d20\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u6bd4\u8f03\u5169\u500b List \u4e4b\u9593\u7684\u5dee\u7570\uff0c\u6216\u8005\u627e\u51fa\u5169\u500b List \u4e4b\u9593\u7684\u76f8\u540c\u9805\u76ee\u3002<\/p>\n<p>\u5728 React.JS \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Array.prototype.filter() \u65b9\u6cd5\u4f86\u8655\u7406 List \u7684\u4ea4\u96c6(intersection)\u3002Array.prototype.filter() \u65b9\u6cd5\u6703\u5c0d List \u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u6307\u5b9a\u7684\u51fd\u5f0f\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684 List\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u7b26\u5408\u689d\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u5169\u500b List\uff0c\u5206\u5225\u70ba ListA \u548c ListB\uff0c\u5176\u4e2d ListA \u5305\u542b [1, 2, 3, 4, 5]\uff0cListB \u5305\u542b [3, 4, 5, 6, 7]\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 Array.prototype.filter() \u65b9\u6cd5\u4f86\u627e\u51fa\u5169\u500b List \u4e4b\u9593\u7684\u4ea4\u96c6\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst ListA = [1, 2, 3, 4, 5];\nconst ListB = [3, 4, 5, 6, 7];\n\nconst intersection = ListA.filter(x => ListB.includes(x));\n\nconsole.log(intersection); \/\/ [3, 4, 5]\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 Array.prototype.filter() \u65b9\u6cd5\u5c0d ListA \u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u6307\u5b9a\u7684\u51fd\u5f0f\uff0c\u4e26\u6aa2\u67e5 ListB \u4e2d\u662f\u5426\u5305\u542b\u8a72\u5143\u7d20\uff0c\u5982\u679c\u5305\u542b\uff0c\u5247\u5c07\u8a72\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u7684 List \u4e2d\uff0c\u6700\u5f8c\u8fd4\u56de\u65b0\u7684 List\uff0c\u5176\u4e2d\u5305\u542b ListA \u548c ListB \u4e4b\u9593\u7684\u4ea4\u96c6\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u4f7f\u7528 ES6 \u7684 Set() \u65b9\u6cd5\u4f86\u8655\u7406 List \u7684\u4ea4\u96c6(intersection)\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst ListA = [1, 2, 3, 4, 5];\nconst ListB = [3, 4, 5, 6, 7];\n\nconst intersection = [...new Set(ListA)].filter(x => ListB.includes(x));\n\nconsole.log(intersection); \/\/ [3, 4, 5]\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 ES6 \u7684 Set() \u65b9\u6cd5\u5c07 ListA \u8f49\u63db\u70ba Set\uff0c\u7136\u5f8c\u4f7f\u7528 Array.prototype.filter() \u65b9\u6cd5\u5c0d ListB \u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u6307\u5b9a\u7684\u51fd\u5f0f\uff0c\u4e26\u6aa2\u67e5 ListA \u4e2d\u662f\u5426\u5305\u542b\u8a72\u5143\u7d20\uff0c\u5982\u679c\u5305\u542b\uff0c\u5247\u5c07\u8a72\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u7684 List \u4e2d\uff0c\u6700\u5f8c\u8fd4\u56de\u65b0\u7684 List\uff0c\u5176\u4e2d\u5305\u542b ListA \u548c ListB \u4e4b\u9593\u7684\u4ea4\u96c6\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c\u4f7f\u7528 React.JS \u4f86\u8655\u7406 List \u7684\u4ea4\u96c6(intersection)\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u6bd4\u8f03\u5169\u500b List \u4e4b\u9593\u7684\u5dee\u7570\uff0c\u6216\u8005\u627e\u51fa\u5169\u500b List \u4e4b\u9593\u7684\u76f8\u540c\u9805\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39ReactList\u7684\u4ea4\u96c6(Intersection)\u529f\u80fd\uff0c\u8a73\u7d30\u89e3\u91cb\u5982\u4f55\u4f7f\u7528ReactList\u7684intersection()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-6486","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1GC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6486"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6486\/revisions"}],"predecessor-version":[{"id":6487,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6486\/revisions\/6487"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}