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 发布文章使用:只允许注册用户才可以访问!
how to remove duplicates of an array by using js reduce function的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 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 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- E4.IO.pry/0-IO.break!/1动态打点调试
IO.pry/0 IO.inspect只能在静态地打印指定的变量,Elixir的shell还可以使用IO.pry/0与IO.break!/1实现更灵活的调试方法. 假如你想查看一下函数的某个位置到底发 ...
- Error Code: 2006 - MySQL 5.7 server has gone away
使用 Navicat 执行 sql 脚本失败 出现 Error Code: 2006 - MySQL server has gone away 原因 当MySQL客户端或mysqld服务器收到大于ma ...
- (001)每日SQL学习:关于UNION的使用
union内部必须有相同的列或者相同的数据类型,同时,每条 SELECT 语句中的列的顺序必须相同.union合并了select的结果集. union 与union all的不同: union合并了重 ...
- compare-algorithms-for-heapqsmallest
Compare algorithms for heapq.smallest « Python recipes « ActiveState Code http://code.activestate.co ...
- 上海某小公司面试题:Java线程池来聊聊
<对线面试官>系列目前已经连载11篇啦!进度是一周更新两篇,欢迎持续关注 [对线面试官]Java注解 [对线面试官]Java泛型 [对线面试官] Java NIO [对线面试官]Java反 ...
- Java——Math,Set,List,map相关练习
声明一个Set集合,只能保存Double类型的数据, 保存10个随机100以内的数, 找出最大值和最小值,打印输出. public static void main(String[] args) { ...
- Java 高并发 解决方案
1.HTML静态化 2.图片服务器分离 3.数据库集群和库表散列 4.缓存 5.镜像 6.负载均衡 1)硬件四层交换 2)软件四层交换 一:高并发高负载类网站关注点之数据库 二:高并发高负载网站的系统 ...
- Cobaltstrike去除特征
出品|MS08067实验室(www.ms08067.com) 本文作者:BlackCat(Ms08067实验室内网小组成员) 前言: 红蓝对抗的时候,如果未修改CS特征.容易被蓝队溯源. 去特征的几种 ...
- DEDECMS:DEDE整合(UEditor)百度编辑器以后,栏目内容、单页无法保存内容的bug处理方法
已经整合过百度编辑器的站长们或许会发现,在编辑单页文档和栏目内容的时候,百度编辑器不能够保存新增或已修改数据,经过排查后发现问题出现在catalog_edit.htm.catalog_add.htm这 ...
- DEDECMS:修改网站底部出现的POWER BY DEDECMS
在include/dedesql.classs.php文件中找到第588行: $arrs1 = array(0x63,0x66,0x67,0x5f,0x70,0x6f,0x77,0x65,0x72,0 ...