{"id":599,"date":"2024-01-03T09:01:01","date_gmt":"2024-01-03T01:01:01","guid":{"rendered":""},"modified":"2024-01-03T09:01:01","modified_gmt":"2024-01-03T01:01:01","slug":"\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u4e4bincludes()","status":"publish","type":"post","link":"https:\/\/mushiming.com\/599.html","title":{"rendered":"\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u4e4bincludes()"},"content":{"rendered":"
\u793a\u4f8b1\uff1a#<\/strong><\/strong><\/p>\n \u53bb\u91cd\u5904\u7406\uff1a#<\/strong><\/strong><\/p>\n \u793a\u4f8b2\uff1a#<\/strong><\/strong><\/p>\n \u5220\u9664 id \u76f8\u540c\u7684\u6570\u7ec4\u5bf9\u8c61 \u505a\u53bb\u91cd\u5904\u7406\uff1a#<\/strong><\/strong><\/p>\n \u8fd9\u6837\u5c31\u8fbe\u5230\u4e86\u6211\u4eec\u8981\u53bb\u91cd\u7684\u6570\u7ec4\uff0c\u6839\u636e\u67d0\u9879\u53bb\u91cd\u6570\u7ec4\u4e2d\u7684\u5bf9\u8c61\u6570\u636e\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u4e4bincludes()\u793a\u4f8b\uff1a#letArr=['1','2','2','3','3']_includes\u6570\u7ec4\u53bb\u91cd","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/599"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}let arr = ['1', '2', '2', '3', '3']<\/code><\/pre>\n
let newArr = []\n\/\/ \u5229\u7528includes \u68c0\u67e5\u65b0\u6570\u7ec4\u662f\u5426\u5305\u542b\u539f\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\uff0c\u5982\u679c\u4e0d\u5305\u542b\uff0c\u5c31push\u8fdb\u53bb\narr.forEach(item => {\n if(!newArr.includes(item)) {\n newArr.push(item)\n }\n}) \nconsole.log(newArr) \/\/ \u6253\u5370\u7ed3\u679c ['1', '2' ,'3']<\/code><\/pre>\n
let arr = [\n {\n id: 1,\n name: '\u5c0f\u660e'\n },\n {\n id: 2,\n name: '\u5c0f\u5170'\n },\n {\n id: 1,\n name: '\u5c0f\u660e'\n }\n]<\/code><\/pre>\n
let newArr = []\n\/\/ \u5229\u7528includes \u6839\u636e\u539f\u6570\u7ec4\u4e2d\u8981\u53bb\u91cd\u7684\u5b57\u6bb5\uff08\u6b64\u5904\u662f\u6839\u636eid\u5224\u65ad\uff09\uff0c\u68c0\u67e5\u65b0\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u9879\u662f\u5426\u6709id\u8fd9\u4e00\u9879\u7684\u503c\uff0c\u6ca1\u6709\u5c31push\u8fdb\u53bb\narr.forEach(item => {\n if(!newArr.includes(item.id)) {\n newArr.push(item)\n }\n}) \nconsole.log(newArr) \/\/ \u6253\u5370\u7ed3\u679c[{id: 1, name: '\u5c0f\u660e'},{id: 2, name: '\u5c0f\u5170'}]<\/code><\/pre>\n