{"id":595,"date":"2024-01-05T09:01:01","date_gmt":"2024-01-05T01:01:01","guid":{"rendered":""},"modified":"2024-01-05T09:01:01","modified_gmt":"2024-01-05T01:01:01","slug":"js\u4e2d\u5e38\u89c1\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 && \u5f53\u6570\u7ec4\u4e2d\u6bcf\u9879\u4e3aobj\u65f6\uff0c\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/595.html","title":{"rendered":"js\u4e2d\u5e38\u89c1\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 && \u5f53\u6570\u7ec4\u4e2d\u6bcf\u9879\u4e3aobj\u65f6\uff0c\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5"},"content":{"rendered":"

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

\u6570\u7ec4\u53bb\u91cd\u7684\u65b9\u6cd5<\/h2>\n

\u5f53\u6570\u7ec4\u4e2d\u6bcf\u9879\u4e3aobj\u65f6\uff08\u6570\u636e\u7ed3\u6784\u4e3a[{}, {}, {}]\uff09\uff0c\u53bb\u91cd\u65b9\u6cd5\uff1a<\/em><\/h4>\n
let arr = [{ key:1, name: '\u8d75\u56db' }, { key:2, name: '\u5f20\u4e09' }, { key:1, name: '\u8d75\u56db' }];\nlet result = [];\nlet obj = {};\narr.map(item => {\n\tif (!obj[item.key]) {\n\t\tobj[item.key] = item.key;\n\t\tresult.push(item);\n\t}\n});\nconsole.log(result);\n\/\/ [{key: 1, name: '\u8d75\u56db'},{key: 2, name: '\u5f20\u4e09'}]\n\n<\/code><\/pre>\n

1\u3001\u5229\u7528ES6 Set\u53bb\u91cd\uff08ES6\u4e2d\u6700\u5e38\u7528\uff09<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span> (<\/span>arr<\/span>)<\/span> { \n   <\/span>\n      return<\/span> Array.<\/span>from<\/span>(<\/span>new<\/span> Set<\/span>(<\/span>arr)<\/span>)<\/span> \/\/Set()\u65b9\u6cd5\u6253\u5370\u51fa\u6765\u662f\u4e2a\u6ca1\u6709\u91cd\u590d\u503c\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u8981\u7528Array.from\u65b9\u6cd5\u628a\u5b83\u8f6c\u6210\u6570\u7ec4\u540e\u8fd4\u56de<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n\n\/\/\u7b80\u5199\uff1a<\/span>\nlet<\/span> arr1 =<\/span> [<\/span>...<\/span>new<\/span> Set<\/span>(<\/span>arr)<\/span>]<\/span>\nconsole.<\/span>log<\/span>(<\/span>arr1)<\/span>;<\/span>\n\n\/\/\u6269\u5c55\uff1a\u5c06\u4efb\u610f\u4e2a\u6570\u7ec4\u5408\u5e76\uff0c\u5e76\u53bb\u91cd<\/span>\nfunction<\/span> merge<\/span>(<\/span>)<\/span> { \n   <\/span>\n    return<\/span> Array.<\/span>from<\/span>(<\/span>new<\/span> Set<\/span>(<\/span>arguments[<\/span>0<\/span>]<\/span>.<\/span>concat<\/span>(<\/span>...<\/span>arguments)<\/span>)<\/span>)<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>merge<\/span>(<\/span>[<\/span>1<\/span>,<\/span> 2<\/span>,<\/span> 3<\/span>,<\/span> 4<\/span>]<\/span>,<\/span> [<\/span>5<\/span>,<\/span> 6<\/span>,<\/span> 1<\/span>,<\/span> 2<\/span>]<\/span>,<\/span> [<\/span>5<\/span>,<\/span> 6<\/span>,<\/span> 7<\/span>,<\/span> 8<\/span>,<\/span> 9<\/span>]<\/span>)<\/span>)<\/span>\n\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[ 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/span>\n<\/code><\/pre>\n

2\u3001\u5229\u7528for\u5d4c\u5957for\uff0c\u7136\u540esplice\u53bb\u91cd\uff08ES5\u4e2d\u6700\u5e38\u7528\uff09<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span>{ \n   <\/span>            \n  for<\/span>(<\/span>var<\/span> i=<\/span>0<\/span>;<\/span> i<<\/span>arr.<\/span>length;<\/span> i++<\/span>)<\/span>{ \n   <\/span>\n    for<\/span>(<\/span>var<\/span> j=<\/span>i+<\/span>1<\/span>;<\/span> j<<\/span>arr.<\/span>length;<\/span> j++<\/span>)<\/span>{ \n   <\/span>\n      if<\/span>(<\/span>arr[<\/span>i]<\/span>==<\/span>arr[<\/span>j]<\/span>)<\/span>{ \n   <\/span> \/\/\u7b2c\u4e00\u4e2a\u7b49\u540c\u4e8e\u7b2c\u4e8c\u4e2a\uff0csplice\u65b9\u6cd5\u5220\u9664\u7b2c\u4e8c\u4e2a<\/span>\n        arr.<\/span>splice<\/span>(<\/span>j,<\/span>1<\/span>)<\/span>;<\/span>\n        j--<\/span>;<\/span>\n      }<\/span>\n    }<\/span>\n}<\/span>\n  return<\/span> arr;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n<\/code><\/pre>\n

3\u3001\u5229\u7528indexOf\u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n\/\/\u6027\u80fd\u4f18\u5316\uff0c\u9996\u5148\u5224\u65ad\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\uff0c\u4e0d\u662farray\uff0c\u76f4\u63a5\u62a5\u9519\u5e76\u8fd4\u56de<\/span>\n  if<\/span> (<\/span>!<\/span>Array.<\/span>isArray<\/span>(<\/span>arr)<\/span>)<\/span> { \n   <\/span>\n    console.<\/span>log<\/span>(<\/span>'type error!'<\/span>)<\/span>\n    return<\/span>\n  }<\/span>\n  var<\/span> array =<\/span> [<\/span>]<\/span>;<\/span>\n  for<\/span> (<\/span>var<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>array .<\/span>indexOf<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span> ===<\/span> -<\/span>1<\/span>)<\/span> { \n   <\/span>\n        array .<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>\n    }<\/span>\n  }<\/span>\n  return<\/span> array;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n<\/code><\/pre>\n

4\u3001\u5229\u7528sort()\u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n\/\/\u6027\u80fd\u4f18\u5316\uff0c\u9996\u5148\u5224\u65ad\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\uff0c\u4e0d\u662farray\uff0c\u76f4\u63a5\u62a5\u9519\u5e76\u8fd4\u56de<\/span>\n  if<\/span> (<\/span>!<\/span>Array.<\/span>isArray<\/span>(<\/span>arr)<\/span>)<\/span> { \n   <\/span>\n    console.<\/span>log<\/span>(<\/span>'type error!'<\/span>)<\/span>\n    return<\/span>;<\/span>\n  }<\/span>\n  arr =<\/span> arr.<\/span>sort<\/span>(<\/span>)<\/span>\n  var<\/span> arrry=<\/span> [<\/span>arr[<\/span>0<\/span>]<\/span>]<\/span>;<\/span>\n  for<\/span> (<\/span>var<\/span> i =<\/span> 1<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>arr[<\/span>i]<\/span> !==<\/span> arr[<\/span>i-<\/span>1<\/span>]<\/span>)<\/span> { \n   <\/span>\n      arrry.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n    }<\/span>\n  }<\/span>\n  return<\/span> arrry;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n<\/code><\/pre>\n

5\u3001\u5229\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0d\u80fd\u76f8\u540c\u7684\u7279\u70b9\u8fdb\u884c\u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n  if<\/span> (<\/span>!<\/span>Array.<\/span>isArray<\/span>(<\/span>arr)<\/span>)<\/span> { \n   <\/span>\n  \/\/\u6027\u80fd\u4f18\u5316\uff0c\u9996\u5148\u5224\u65ad\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\uff0c\u4e0d\u662farray\uff0c\u76f4\u63a5\u62a5\u9519\u5e76\u8fd4\u56de<\/span>\n    console.<\/span>log<\/span>(<\/span>'type error!'<\/span>)<\/span>\n    return<\/span>\n  }<\/span>\n  var<\/span> arrry=<\/span> [<\/span>]<\/span>;<\/span>\n  var<\/span>  obj =<\/span> { \n   <\/span>}<\/span>;<\/span>\n  for<\/span> (<\/span>var<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>!<\/span>obj[<\/span>arr[<\/span>i]<\/span>]<\/span>)<\/span> { \n   <\/span>\n      arrry.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>\n      obj[<\/span>arr[<\/span>i]<\/span>]<\/span> =<\/span> 1<\/span>\n    }<\/span> else<\/span> { \n   <\/span>\n      obj[<\/span>arr[<\/span>i]<\/span>]<\/span>++<\/span>\n    }<\/span>\n  }<\/span>\n  return<\/span> arrry;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n<\/code><\/pre>\n

6\u3001\u5229\u7528includes\u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n    \/\/\u6027\u80fd\u4f18\u5316\uff0c\u9996\u5148\u5224\u65ad\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\uff0c\u4e0d\u662farray\uff0c\u76f4\u63a5\u62a5\u9519<\/span>\n  if<\/span> (<\/span>!<\/span>Array.<\/span>isArray<\/span>(<\/span>arr)<\/span>)<\/span> { \n   <\/span>\n    console.<\/span>log<\/span>(<\/span>'type error!'<\/span>)<\/span>\n    return<\/span>\n  }<\/span>\n    \/\/\u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5faa\u73af\u904d\u5386\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u503c\uff0c\u5982\u679c\u5728\u521b\u5efa\u7684\u6570\u7ec4\u4e2d\u6ca1\u6709\u6b63\u5728\u5faa\u73af\u7684\u503c\u65f6\uff0c\u5219\u6dfb\u52a0\u8fdb\u521b\u5efa\u7684\u6570\u7ec4\uff0c\u5426\u5219\uff0c\u5c31\u8df3\u8fc7<\/span>\n  var<\/span> array =<\/span>[<\/span>]<\/span>;<\/span> \n  for<\/span>(<\/span>var<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n    if<\/span>(<\/span> !<\/span>array.<\/span>includes<\/span>(<\/span> arr[<\/span>i]<\/span>)<\/span> )<\/span> { \n   <\/span>\/\/includes \u68c0\u6d4b\u6570\u7ec4\u662f\u5426\u6709\u67d0\u4e2a\u503c<\/span>\n      array.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n    }<\/span>\n  }<\/span>\n  return<\/span> array\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n<\/code><\/pre>\n

7\u3001\u5229\u7528hasOwnProperty\u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n  var<\/span> obj =<\/span> { \n   <\/span>}<\/span>;<\/span>\n  return<\/span> arr.<\/span>filter<\/span>(<\/span>function<\/span>(<\/span>item,<\/span> index,<\/span> arr<\/span>)<\/span>{ \n   <\/span>\n    return<\/span> obj.<\/span>hasOwnProperty<\/span>(<\/span>typeof<\/span> item +<\/span> item)<\/span> ?<\/span> false<\/span> :<\/span> (<\/span>obj[<\/span>typeof<\/span> item +<\/span> item]<\/span> =<\/span> true<\/span>)<\/span>\n  }<\/span>)<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[ 1, 'true', true, 15, false, null, NaN, 'NaN', 0, 'a', {} ]<\/span>\n<\/code><\/pre>\n

8\u3001\u5229\u7528filter\u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>'true'<\/span>,<\/span>'true'<\/span>,<\/span>true<\/span>,<\/span>true<\/span>,<\/span>15<\/span>,<\/span>15<\/span>,<\/span>false<\/span>,<\/span>false<\/span>,<\/span> null<\/span>,<\/span>null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span>'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span>{ \n   <\/span>}<\/span>,<\/span>{ \n   <\/span>}<\/span>]<\/span>;<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n      return<\/span> arr.<\/span>filter<\/span>(<\/span>function<\/span>(<\/span>item,<\/span> index,<\/span> arr<\/span>)<\/span> { \n   <\/span>\n        \/\/\u5f53\u524d\u5143\u7d20\uff0c\u5728\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15==\u5f53\u524d\u7d22\u5f15\u503c\uff0c\u5426\u5219\u8fd4\u56de\u5f53\u524d\u5143\u7d20<\/span>\n        return<\/span> arr.<\/span>indexOf<\/span>(<\/span>item,<\/span> 0<\/span>)<\/span> ===<\/span> index;<\/span>\n      }<\/span>)<\/span>;<\/span>\n    }<\/span>\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[ 1, 'true', true, 15, false, null, 'NaN', 0, 'a', {}, {} ]<\/span>\n<\/code><\/pre>\n

9\u3001\u5229\u7528Map\u6570\u636e\u7ed3\u6784\u53bb\u91cd<\/p>\n

function<\/span> arrayNonRepeatfy<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n      let<\/span> map =<\/span> new<\/span> Map<\/span>(<\/span>)<\/span>;<\/span>\n      let<\/span> array =<\/span> new<\/span> Array<\/span>(<\/span>)<\/span>;<\/span>  \/\/ \u6570\u7ec4\u7528\u4e8e\u8fd4\u56de\u7ed3\u679c<\/span>\n      for<\/span> (<\/span>let<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n        if<\/span>(<\/span>map .<\/span>has<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>)<\/span> { \n   <\/span>  \/\/ \u5982\u679c\u6709\u8be5key\u503c<\/span>\n          map .<\/span>set<\/span>(<\/span>arr[<\/span>i]<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\n        }<\/span> else<\/span> { \n   <\/span>\n        map .<\/span>set<\/span>(<\/span>arr[<\/span>i]<\/span>,<\/span> false<\/span>)<\/span>;<\/span>   \/\/ \u5982\u679c\u6ca1\u6709\u8be5key\u503c<\/span>\n          array .<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n        }<\/span>\n      }<\/span>\n      return<\/span> array ;<\/span>\n    }<\/span>\n<\/code><\/pre>\n

10\u3001\u5229\u7528reduce+includes \u53bb\u91cd<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span> 1<\/span>,<\/span> 'true'<\/span>,<\/span> 'true'<\/span>,<\/span> true<\/span>,<\/span> true<\/span>,<\/span> 15<\/span>,<\/span> 15<\/span>,<\/span> false<\/span>,<\/span> false<\/span>,<\/span> null<\/span>,<\/span> null<\/span>,<\/span> NaN<\/span>,<\/span> NaN<\/span>,<\/span> 'NaN'<\/span>,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> 'a'<\/span>,<\/span> 'a'<\/span>,<\/span> { \n   <\/span>}<\/span>,<\/span> { \n   <\/span>}<\/span>]<\/span>;<\/span>\n\n\/\/\u5229\u7528reduce\u65b9\u6cd5 \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0cprev\u4f5c\u4e3a\u4e0a\u4e00\u6b21\u8ba1\u7b97\u7684\u7ed3\u679c\u4f20\u7ed9\u4e0b\u4e00\u6b21\u8fd0\u7b97<\/span>\n\/\/\u5728\u6bcf\u6b21\u8ba1\u7b97\u4e2d\uff0c\u5229\u7528\u6570\u7ec4\u7684includes\u65b9\u6cd5\uff0c\u5224\u65ad\u5f53\u524dprev\u4e2d\u662f\u5426\u542b\u6709cur\uff0c\u5982\u679c\u6709\uff0c\u5219\u8fd4\u56de\u5f53\u524dprev\uff1b\u5426\u5219\uff0c\u5229\u7528\u5c55\u5f00\u8fd0\u7b97\u7b26\u5408\u5e76prev\u548ccur\u503c\uff0c\u6700\u540e\uff0c\u8fd4\u56dereduce\u65b9\u6cd5\u8ba1\u7b97\u7684\u7ed3\u679c<\/span>\n\nfunction<\/span> unique<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n    return<\/span> arr.<\/span>reduce<\/span>(<\/span>(<\/span>prev,<\/span> cur<\/span>)<\/span> =><\/span> { \n   <\/span>\n        return<\/span> prev.<\/span>includes<\/span>(<\/span>cur)<\/span> ?<\/span> prev :<\/span> [<\/span>...<\/span>prev,<\/span> cur]<\/span>\n    }<\/span>,<\/span> [<\/span>]<\/span>)<\/span>  \/\/\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53reduce\u65b9\u6cd5\u6709\u7b2c2\u4e2a\u53c2\u6570\u65f6\uff0c\u5b83\u4e3aprev\u7684\u521d\u59cb\u503c<\/span>\n}<\/span>\n\nconsole.<\/span>log<\/span>(<\/span>unique<\/span>(<\/span>arr)<\/span>)<\/span>\n\/\/\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n\/\/[1, \"true\", true, 15, false, null, NaN, \"NaN\", 0, \"a\", {}, {}]<\/span>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"js\u4e2d\u5e38\u89c1\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 && \u5f53\u6570\u7ec4\u4e2d\u6bcf\u9879\u4e3aobj\u65f6\uff0c\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5js\u4e2d\u5e38\u89c1\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5&&\u5f53\u6570\u7ec4\u4e2d\u6bcf\u9879\u4e3aobj\u65f6\uff0c\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\/595"}],"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=595"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/595\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}