how to remove duplicates of an array by using js reduce function

???

  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. return !acc.includes(item) ? acc.push(item) : acc;
  5. // TypeError: acc.includes is not a function
  6. }, []);

  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-11-24
  8. * @modified
  9. *
  10. * @description
  11. * @description
  12. * @difficulty Easy Medium Hard
  13. * @complexity O(n)
  14. * @time O(n)
  15. * @augments
  16. * @example
  17. * @link
  18. * @solutions
  19. *
  20. * @best_solutions
  21. *
  22. */
  23. const log = console.log;
  24. function func1() {
  25. return 1;
  26. }
  27. const test1 = func1();
  28. log(`test1 =`, test1);
  29. // test1 = 1
  30. function func2() {
  31. let arr = [];
  32. let item = 2;
  33. log(`arr.includes(item)`, arr.includes(item), !arr.includes(item));
  34. // arr.includes(item) false true
  35. log(`return`, (!arr.includes(item) ? arr.push(item) : arr));
  36. // return 1
  37. // ??? 三元表达式没有,返回值?
  38. return (!arr.includes(item) ? arr.push(item) : arr);
  39. // return !arr.includes(item) ? arr.push(item) : arr;
  40. }
  41. const test2 = func2();
  42. log(`test2 =`, test2);
  43. // test2 = 1
  44. function func() {
  45. let arr = [];
  46. let item = 2;
  47. !arr.includes(item) ? arr.push(item) : arr;
  48. return arr;
  49. }
  50. const test = func();
  51. log(`test =`, test);
  52. // [2]
  53. /*
  54. // error
  55. const test1 = isValid(`((}}`);
  56. // ok
  57. const test2 = isValid(`()[]{}`);
  58. log(`test =`, test1 ? `` : ``);
  59. log(`test ok =`, test2 ? `` : ``);
  60. */

  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. if(!acc.includes(item)) {
  5. acc.push(item);
  6. }
  7. return acc;
  8. }, []);

flat & remove duplicates


  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-11-24
  8. * @modified
  9. *
  10. * @description
  11. * @description
  12. * @difficulty Easy Medium Hard
  13. * @complexity O(n)
  14. * @time O(n)
  15. * @augments
  16. * @example
  17. * @link
  18. * @solutions
  19. *
  20. * @best_solutions
  21. *
  22. */
  23. const log = console.log;
  24. // flat & remove duplication
  25. const arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  26. // const test = arr.flat(Infinity).reduce((acc, item) => !acc.includes(item) ? acc.push(item) : acc, []);
  27. const test = arr.flat(Infinity).reduce((acc, item) => {
  28. log(`acc`, acc, acc.includes)
  29. acc = !acc.includes(item) ? acc.push(item) : acc;
  30. // TypeError: acc.includes is not a function
  31. // if(!acc.includes(item)) {
  32. // acc.push(item);
  33. // }
  34. return acc;
  35. }, []);
  36. log(`test = `, test);
  37. const arr2 = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  38. // const test2 = arr.flat(Infinity).reduce((acc, item) => acc.indexOf(item) < 0 ? acc.push(item) : acc, []);
  39. const test2 = arr2.flat(Infinity).reduce((acc, item) => {
  40. log(`acc`, acc, acc.indexOf)
  41. // acc = acc.indexOf(item) < 0 ? acc.push(item) : acc;
  42. // TypeError: acc.indexOf is not a function
  43. if(acc.indexOf(item) < 0) {
  44. acc.push(item);
  45. }
  46. return acc;
  47. }, []);
  48. log(`test2 = `, test2);
  49. /*
  50. // error
  51. const test1 = isValid(`((}}`);
  52. // ok
  53. const test2 = isValid(`()[]{}`);
  54. log(`test =`, test1 ? `` : ``);
  55. log(`test ok =`, test2 ? `` : ``);
  56. */

[...acc, item] 返回新数组

  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. return acc.includes(item) ? acc : acc.push(item);
  5. }, []);

  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. return acc.includes(item) ? acc : [...acc, item];
  5. }, []);

问题分析

array.push(item) 返回新数组的新长度

[...array, item] 返回一个新数组

  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. // [...array, item] 返回一个新数组
  5. return acc.includes(item) ? acc : [...acc, item];
  6. }, []);
  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. acc.includes(item) ? acc : acc.push(item);
  5. // OK,返回一个新数组
  6. return acc;
  7. }, []);
  1. arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
  2. arr.flat(Infinity).reduce((acc, item) => {
  3. console.log(`acc`, acc, acc.includes)
  4. // Error ??? array.push(item) 返回新数组的新长度
  5. return acc.includes(item) ? acc : acc.push(item);
  6. }, []);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

refs

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


how to remove duplicates of an array by using js reduce function的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  8. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. 转 jmeter录制https请求

    jmeter录制https请求  文章转自:https://www.cnblogs.com/zhengna/p/10180998.html 工具:Jmeter4.0 + Java1.8 需求:对某ht ...

  2. MySQL 中的临时表

    在使用 explain 解析一个 sql 时,有时我们会发现在 extra 列上显示 using temporary ,这表示这条语句用到了临时表,那么临时表究竟是什么?它又会对 sql 的性能产生什 ...

  3. 小白都看得懂的Javadoc使用教程

    Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...

  4. 扩展欧几里得(exgcd)及其应用

    定义 扩展欧几里得算法是用来在已知一组 \((a,b)\) 的时,求解一组 \((x,y)\) 使得 \[ax+by=gcd(a,b) \] 思想 and 板子 根据相关的知识可以得到 \[gcd(a ...

  5. 用友GRP-u8 SQL注入

    POST /Proxy HTTP/1.1 Accept: Accept: */* Content-Type: application/x-www-form-urlencoded User-Agent: ...

  6. 小米和MAC触摸板手势汇总

    小米的触摸手势: 左键:单指单击 右键:双指单击 选取并打开:单指双击 滚动页面:双指 移动 拖拽项目:双击并拖拽 放大/缩小:双指张开,双指捏合 MAC触摸板手势: http://www.cr173 ...

  7. samba 随笔

    SElinux以及防火墙的关闭 关闭SELinux的方法: 修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启. 如果不想重启 ...

  8. java 将内容写入文件 txt

    @Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...

  9. UML——基本结构

    一.宏观导图 学习UML的时候我们首先要把握好她的结构,基本上好料都在里面了.最重要的是构造块的学习. 公共机制:是为了让我们更加清楚的描述UML的各种关系.图.事物等. 规则:和语法的意思差不多,就 ...

  10. 调用个别f5 负载端口为80的vs时,返回值为空的问题

    现状: vs负载端口为80并添加XFF,pool包含2个member,member的monitor端口为80&9000. 故障现象: 应用同事描述说再完全复制了一个member并添加到pool ...