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 ...
随机推荐
- ryu—流量监视
1. 代码解析 ryu/app/simple_monitor_13.py: from operator import attrgetter from ryu.app import simple_swi ...
- 攻击JWT的一些方法
JWT安全隐患之绕过访问控制 https://mp.weixin.qq.com/s/xe8vOVhaysmgvxl-A3nkBA 记录一次JWT的越权渗透测试 https://mp.weixin.qq ...
- 2.kafka架构深入——生产者
一个topic有多个partition,每个partition又有多个副本,在这些副本中又有一个leader和多个follower. 1)分区的原因 (1)方便在集群中扩展,每个Partition可以 ...
- Base64原理 bits 3->4 8bits/byte-->6bits/byte
实践: window.btoa('a')a YQ==abcdef YWJjZGVmabc YWJjab YWI= https://en.wikipedia.org/wiki/Base64 The Ba ...
- 自修改代码 on the fly 动态编译 即时编译 字节码
https://zh.wikipedia.org/wiki/自修改代码 自修改代码(Self-modifying code)是指程序在运行期间(Run time)修改自身指令.可能的用途有:病毒利用此 ...
- Understanding go.sum and go.mod file in Go
https://golangbyexample.com/go-mod-sum-module/ Understanding go.sum and go.mod file in Go (Golang) – ...
- Linux 中的文件属性
文件属性 d 开头是: 目录文件.l 开头是: 符号链接(指向另一个文件,类似于瘟下的快捷方式).s 开头是: 套接字文件(sock).b 开头是: 块设备文件,二进制文件.c 开头是: 字符设备文 ...
- 洛谷 P6362 平面欧几里得最小生成树
题目描述 平面上有 \(n\) 个点,第 \(i\) 个点坐标为 \((x_i, y_i)\).连接 \(i, j\) 两点的边权为 \(\sqrt{(x_i - x_j) ^ 2 + (y_i - ...
- HBase,以及GeoMesa设计基于HBase的设计分析,从数据模型到典型查询场景,最后进行RowKey设计
GeoMesa设计基于HBase的设计分析,从数据模型到典型查询场景,最后进行RowKey设计 一.HBase 基本概念 理解KeyValue KeyValue多版本 列定义(1) 列定义(2) Co ...
- servelet 实现Post接口访问
先上代码: package com.jovtec.galaxy.mailbox; import java.io.BufferedReader; import java.io.IOException; ...