{"id":603,"date":"2024-01-01T09:01:01","date_gmt":"2024-01-01T01:01:01","guid":{"rendered":""},"modified":"2024-01-01T09:01:01","modified_gmt":"2024-01-01T01:01:01","slug":"\u4e09\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/603.html","title":{"rendered":"\u4e09\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4f7f\u7528Set<\/strong><\/p>\n

var array1 = [1,\"1\",2,1,3,4,5,2,\"1\",\"2\"];\nfunction unique1(arr){\n  return [...new Set(arr)]\n}\nvar newArray1 = unique1(array1);\nconsole.log(newArray1); \/\/ [ 1, \"1\", 2, 3, 4, 5, \"2\" ]\n<\/code><\/pre>\n

\u4f7f\u7528filter<\/strong><\/p>\n

var array2 = [\"10\",20,10,30,40,50,20,\"10\",\"20\"];\nfunction unique2(arr){\n  var obj = {};\n  return arr.filter((item) => {\n    if(!obj[item]){\n      obj[item] = true;\n      return true;\n    }\n  })\n}\nvar newArray2 = unique2(array2);\nconsole.log(newArray2); \/\/ [ \"10\", 20, 30, 40, 50 ]\n<\/code><\/pre>\n

\u8be5\u65b9\u6cd5\u5b58\u5728\u4e00\u4e2a\u5f0a\u7aef\uff0c\u5f53\u6570\u7ec4\u4e3a[1,\u201c1\u201d,2]\u65f6\uff0c\u7ed3\u679c\u4e3a[1,2]\u3002\u5f53\u6570\u7ec4\u4e3a[\u201c1\u201d,1,2]\u65f6\uff0c\u7ed3\u679c\u4e3a[\u201c1\u201d,2]\u3002<\/p>\n

\u4f7f\u7528IndexOf<\/strong><\/p>\n

var array3 = [100,\"100\",200,100,300,400,500,200,\"100\",\"200\"];\nfunction unique3(arr){\n  var result = [];\n  arr.forEach((item) => {\n    if(result.indexOf(item) === -1){\n      result.push(item)\n    }\n  })\n  return result;\n}\nvar newArray3 = unique3(array3);\nconsole.log(newArray3); \/\/ [ 100, \"100\", 200, 300, 400, 500, \"200\" ]\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4e09\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u4f7f\u7528Set\u3001filter\u3001indexOf\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd_newarray\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\/603"}],"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=603"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}