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 ...
随机推荐
- 转 jmeter录制https请求
jmeter录制https请求 文章转自:https://www.cnblogs.com/zhengna/p/10180998.html 工具:Jmeter4.0 + Java1.8 需求:对某ht ...
- MySQL 中的临时表
在使用 explain 解析一个 sql 时,有时我们会发现在 extra 列上显示 using temporary ,这表示这条语句用到了临时表,那么临时表究竟是什么?它又会对 sql 的性能产生什 ...
- 小白都看得懂的Javadoc使用教程
Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...
- 扩展欧几里得(exgcd)及其应用
定义 扩展欧几里得算法是用来在已知一组 \((a,b)\) 的时,求解一组 \((x,y)\) 使得 \[ax+by=gcd(a,b) \] 思想 and 板子 根据相关的知识可以得到 \[gcd(a ...
- 用友GRP-u8 SQL注入
POST /Proxy HTTP/1.1 Accept: Accept: */* Content-Type: application/x-www-form-urlencoded User-Agent: ...
- 小米和MAC触摸板手势汇总
小米的触摸手势: 左键:单指单击 右键:双指单击 选取并打开:单指双击 滚动页面:双指 移动 拖拽项目:双击并拖拽 放大/缩小:双指张开,双指捏合 MAC触摸板手势: http://www.cr173 ...
- samba 随笔
SElinux以及防火墙的关闭 关闭SELinux的方法: 修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启. 如果不想重启 ...
- java 将内容写入文件 txt
@Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...
- UML——基本结构
一.宏观导图 学习UML的时候我们首先要把握好她的结构,基本上好料都在里面了.最重要的是构造块的学习. 公共机制:是为了让我们更加清楚的描述UML的各种关系.图.事物等. 规则:和语法的意思差不多,就 ...
- 调用个别f5 负载端口为80的vs时,返回值为空的问题
现状: vs负载端口为80并添加XFF,pool包含2个member,member的monitor端口为80&9000. 故障现象: 应用同事描述说再完全复制了一个member并添加到pool ...