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 ...
随机推荐
- 03. struts2中Action配置的各项默认值
Action中的各项默认值 Action各项配置 <action name="helloworld" class="com.liuyong666.action.He ...
- JavaScript中的异步函数
JavaScript中的异步函数 ES8 的 async/await 旨在解决利用异步结构组织代码的问题.为此, ECMAScript 对函数进行了扩展,为其增加了两个新关键字: async 和 aw ...
- JVM虚拟机垃圾回收(GC)算法及优缺点
一.什么是GC GC是jvm的垃圾回收,垃圾回收的规律和原则为: 次数上频繁收集新生区(Young) 次数上较少收集养老区(Old) 基本上不动永久区(Perm) 二.GC算法(分代收 ...
- 指令集架构 x86-64 x86架构的64位拓展,向后兼容于16位及32位的x86架构
https://zh.wikipedia.org/wiki/X86 x86泛指一系列英特尔公司用于开发处理器的指令集架构,这类处理器最早为1978年面市的"Intel 8086"C ...
- .params和query的区别
用法:query要用path来引入,params要用name来引入,接收参数都是类似的,分别是this.$route.query.name和this.$route.params.name.url地址显 ...
- java.lang.IllegalStateException Unable to find a @SpringBootConfiguration错误解决方案
问题描述:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Co ...
- docker(mysql-redmine)
一.安装docker 首先查看自己的版本,我的是centos 版本为 [root@localhost redmine]# uname -r 3.10.0-862.el7.x86_64 移除旧版本 yu ...
- 截止9月20日,xx行动中已知漏洞
VMware Fusion cve-2020-3980权限提升 Apache Cocoon security vulnerability cve-2020-11991 Spring框架RFD(文件下载 ...
- 【疑】接入交换机lacp port-channel连接核心突然中断
现状: 职场网络架构为接入交换机2个端口通过lacp协议的active模式组成port-channel上联到核心. 具体配置如下 接入: 核心: 故障现象: zabbix监控到核心交换机对应该接入交换 ...
- 我用了半年的时间,把python学到了能出书的程度
Python难学吗?不难,我边做项目边学,过了半年就通过了出版社编辑的面试,接到了一本Python选题,并成功出版. 有同学会说,你有编程基础外带项目实践机会,所以学得快.这话不假,我之前的基础确实加 ...