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

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

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


posted @ 2020-11-25 13:21  xgqfrms  阅读(138)  评论(4编辑  收藏  举报