You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const a=[1, 2, 3, 4, [3, 4, [4, 6]]];
const re=[];
function isFlat(arr){
let flat=true;
for(let a of arr){
if(Array.isArray(a)){
flat=false;
}
}
return flat;
}
function flat(arr){
//if this array is flat, just push it
if(isFlat(arr)){
re.push(...arr);
return;
}
//if not flat, check each ele, if flat push, else recursive
for(let a of arr){
if( !Array.isArray(a)){
re.push(a);
}else{
flat(a);
}
}
}
//start
flat(a);
console.log(re)
function MyTypeof(data) {
let str = Object.prototype.toString.call(data);
return str.slice('[object '.length, str.length - 1);
}
var TestArray = [[1,2,2],[1,2,2]];
function UniqueArrar(array){
let DstArray = [];
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (MyTypeof(element) === 'Array'){
let arr = [];
for (let index = 0; index < element.length; index++) {
const e = element[index];
if (arr.indexOf(e) === -1){
arr.push(e);
}
}
DstArray.push(arr);
}else {
if (DstArray.indexOf(element) === -1){
DstArray.push(element);
}
}
}
return DstArray;
}
Activity
linghucq1 commentedon May 8, 2019
有一个兼容性不太好的写法:
参考:Array.prototype.flat
myprelude commentedon Jun 13, 2019
think2011 commentedon Jun 17, 2019
说个兼容性比较好的,当然最简单的方式还是基于 new Set
Damon99999 commentedon Jun 19, 2019
AricZhu commentedon Jun 24, 2019
/*
利用Set来去重
利用递归来处理多维情况
*/
function getNoRepeatArr (arr, set) {
}
Konata9 commentedon Jul 10, 2019
shufangyi commentedon Jul 18, 2019
wyx2014 commentedon Jul 25, 2019
Vi-jay commentedon Jul 26, 2019
AchillesV commentedon Jul 29, 2019
shenger9 commentedon Aug 22, 2019
function distinct(a){
return Array.from(new Set(a))
}
15190408121 commentedon Aug 25, 2019
讲个比较笨的
var arrys = []
function arrs(arr) { // 先把所有的提出来
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i]) === true) {
arrs(arr[i])
} else {
arrys.push(arr[i])
}
}
}
function Arrys(arr, arrys) { // 之后去重
arrs(arr)
return [...new Set(arrys)]
}
Arrys([1, 2, 3, 4, 4, 3, 2, 1, [1, 2, 3, 5, [5, 4, 3, 2, 1]]], arrys)
wsypower commentedon Aug 29, 2019
Zhou-Bill commentedon Aug 30, 2019
27 remaining items
codeyourwayup commentedon Jul 21, 2021
先把数组拍平
HNHED commentedon Sep 3, 2021
amikly commentedon Nov 2, 2021
ES5
ES6
github-cxtan commentedon Feb 17, 2022
function MyTypeof(data) {
let str = Object.prototype.toString.call(data);
return str.slice('[object '.length, str.length - 1);
}
var TestArray = [[1,2,2],[1,2,2]];
function UniqueArrar(array){
let DstArray = [];
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (MyTypeof(element) === 'Array'){
let arr = [];
for (let index = 0; index < element.length; index++) {
const e = element[index];
if (arr.indexOf(e) === -1){
arr.push(e);
}
}
DstArray.push(arr);
}else {
if (DstArray.indexOf(element) === -1){
DstArray.push(element);
}
}
}
return DstArray;
}
yxllovewq commentedon Mar 8, 2022
storm-cao commentedon Mar 11, 2022
wind8866 commentedon Mar 25, 2022
看别人的结果都是一维数组,我这个去重并且保留原数组的格式(空数组去除)
vesere commentedon May 18, 2022
var arr = [
[11,1,2,2],
[3,4,5,5],
[6,7,8,9,10,
[11,12,
[12,12,
[13]
]
]
]
]
// 1、数组扁平化
// 2、去重
// 3、排序
// 方法一:递归 要使用function因为this指向 不然箭头函数指向window
Array.prototype.flat = function(){
// 首先将数组遍历处理
const result = this.map(item => {
if(Array.isArray(item)) {
console.log(item);
console.log('-----------------------------------------------------------------------------------');
return item.flat()
}
return [item]
});
return [].concat(...result)
}
console.log('-----------------------------------------------------------------------------------');
console.log(arr.flat());
const arr1 = [
1, 2, 2, 3, 4, 5,
5, 6, 7, 8, 9, 11,
12, 12, 12, 13
]
const newArr = new Set(arr1)
console.log(newArr); // 是一个对象类型所以需要将其转化为数组类型
console.log(typeof(newArr));
console.log(newArr.has(9));
console.log([...newArr]);
// 去重操作
Array.prototype.uniquee = function(){
return [...new Set(this)]
}
// 定义排序的顺序
const sortfn = (a,b)=>{
return a - b
}
console.log('------------------------------------------------------------------------------------------------');
const newArray = arr.flat().uniquee().sort(sortfn)
console.log(newArray);
xiaoqiangz commentedon May 25, 2022
// 写一个数组去重的方法 支持多维数组
var arr = [1, 2, [2, 3, [3, 4, 5]], 5, 5];
// 1, 升序且去重
console.log(Array.from(new Set(arr.flat(Infinity))).sort((a,b) => a - b))
// 2. reduce
function flat(arr) {
return arr.reduce((prev, cur) => {
return Array.isArray(cur) ? prev.concat(flat(cur)) : prev.concat(cur)
}, [])
}
console.log(Array.from(new Set(flat(arr))))
// 3 递归
function flatArr(arr, result) {
arr.forEach(item => {
if (Array.isArray(item)) {
flatArr(item, result)
} else {
result.push(item)
}
})
return result
}
console.log([...new Set(flatArr(arr, []))])
wenxd commentedon May 27, 2022
xiaoxiaozhiya commentedon May 30, 2022
都要用到ES6的Set对象,保存唯一的值
Sobrium commentedon Sep 12, 2022
function flat(arr, newArr){
arr.forEach(item => {
if({}.toString.call(item) === '[object Array]'){
flat(item, newArr);
}else{
if(newArr.indexOf(item) === -1){
newArr.push(item);
}
}
})
}
function flatArr(arr){
let newArr = [];
flat(arr, newArr);
return newArr;
}
console.log(flatArr([1,2,3,4,[1,2,3],4,[a,b,5,[4,5,6],[1,2]]]))
Rednaxela-LinYi commentedon Jul 21, 2023
。。。人家让你们去重,谁让你们降维了,题目要求难道不是返回一个和原数组结构相同的,去重后的数组吗?
function deSameElement(arr, resMap = {}) {
return arr.reduce((acc, cur) => {
let res = null;
if (Array.isArray(cur)) {
res = deSameElement(cur, resMap);
if (Array.isArray(res) && res.length > 0) {
acc.push(res);
}
} else {
if (!resMap[cur]) {
res = cur;
resMap[cur] = true;
acc.push(res);
}
}
return acc;
}, []);
}
let testArr = [
[1, 2, 3, 4, 5, 6],
[2, 5, 7, 9, 4, 3],
10,
5,
8,
[2, 3, 3],
4,
3,
9,
];
console.log(deSameElement(testArr)); // [ [ 1, 2, 3, 4, 5, 6 ], [ 7, 9 ], 10, 8 ]
pengsir120 commentedon Sep 27, 2024
function arrayFlat(arr) {
return Array.from(new Set(arr.flat(Infinity)))
}
panpanxuebing commentedon Dec 10, 2024