• xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!


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

    ❌ ???

    arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
    
    arr.flat(Infinity).reduce((acc, item) => {
      console.log(`acc`, acc, acc.includes)
      return !acc.includes(item) ? acc.push(item) : acc;
      // ❓❌ TypeError: acc.includes is not a function
    }, []);
    
    
    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-11-24
     * @modified
     *
     * @description
     * @description
     * @difficulty Easy Medium Hard
     * @complexity O(n)
     * @time O(n)
     * @augments
     * @example
     * @link
     * @solutions
     *
     * @best_solutions
     *
     */
    
    const log = console.log;
    
    function func1() {
      return 1;
    }
    
    const test1 = func1();
    log(`test1 =`, test1);
    // test1 = 1
    
    function func2() {
      let arr = [];
      let item = 2;
      log(`arr.includes(item)`, arr.includes(item), !arr.includes(item));
      // arr.includes(item) false true
      log(`return`, (!arr.includes(item) ? arr.push(item) : arr));
      // return 1
      // ❌ ??? 三元表达式没有,返回值?
      return (!arr.includes(item) ? arr.push(item) : arr);
      // return !arr.includes(item) ? arr.push(item) : arr;
    }
    
    const test2 = func2();
    log(`test2 =`, test2);
    // test2 = 1
    
    function func() {
      let arr = [];
      let item = 2;
      !arr.includes(item) ? arr.push(item) : arr;
      return arr;
    }
    
    const test = func();
    log(`test =`, test);
    // [2]
    
    
    
    /*
    
    // error
    const test1 = isValid(`((}}`);
    // ok
    const test2 = isValid(`()[]{}`);
    
    log(`❌test =`, test1 ? `✅` : `❌`);
    log(`✅test ok =`, test2 ? `✅` : `❌`);
    
    */
    
    

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

    flat & remove duplicates

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-11-24
     * @modified
     *
     * @description
     * @description
     * @difficulty Easy Medium Hard
     * @complexity O(n)
     * @time O(n)
     * @augments
     * @example
     * @link 
     * @solutions
     *
     * @best_solutions
     *
     */
    
    const log = console.log;
    
    // flat & remove duplication
    const arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
    
    
    // const test = arr.flat(Infinity).reduce((acc, item) => !acc.includes(item) ? acc.push(item) : acc, []);
    const test = arr.flat(Infinity).reduce((acc, item) => {
      log(`acc`, acc, acc.includes)
      acc = !acc.includes(item) ? acc.push(item) : acc;
      // ❓❌ TypeError: acc.includes is not a function
      // if(!acc.includes(item)) {
      //   acc.push(item);
      // }
      return acc;
    }, []);
    
    log(`test = `, test);
    
    const arr2 = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];
    
    // const test2 = arr.flat(Infinity).reduce((acc, item) => acc.indexOf(item) < 0 ? acc.push(item) : acc, []);
    const test2 = arr2.flat(Infinity).reduce((acc, item) => {
      log(`acc`, acc, acc.indexOf)
      // acc = acc.indexOf(item) < 0 ? acc.push(item) : acc;
      // ❓❌ TypeError: acc.indexOf is not a function
      if(acc.indexOf(item) < 0) {
        acc.push(item);
      }
      return acc;
    }, []);
    
    
    log(`test2 = `, test2);
    
    
    /*
    
    // error
    const test1 = isValid(`((}}`);
    // ok
    const test2 = isValid(`()[]{}`);
    
    log(`❌test =`, test1 ? `✅` : `❌`);
    log(`✅test ok =`, test2 ? `✅` : `❌`);
    
    */
    
    

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

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

    问题分析

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

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

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

    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 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    CSS高级应用参考手册
    通过ClassLoader说明容器热部署实现机制
    自定义类加载器
    zz Ubuntu常用命令大全
    设计模式之Decorator(油漆工)
    zz JNI学习(一)、JNI简介和HelloWorld示例
    类加载器的基本概念
    使用 StAX 解析 XML,第 1 部分: Streaming API for XML (StAX) 简介
    ubuntu apache mod_expires模块
    清空ubuntu 日志
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/14033505.html
Copyright © 2020-2023  润新知