Flatten Array & Array.flat() & Array.flatMap() All In One
Flatten Array & Array.flat() & Array.flatMap() All In One
数组扁平化
https://alligator.io/js/flat-flatmap/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-13
*
* @description 编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组
* @augments
* @example
* @link https://github.com/yygmind/blog/issues/43
*
*/
let arr = [
[1, 2, 2],
[3, 4, 5, 5],
[
6, 7, 8, 9,
[
11, 12,
[
12, 13,
[14]
]
]
],
10,
];
let log = console.log;
const autoFlatArray = (arr = []) => {
let result = [];
const flatArray = (arr = []) => {
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (Array.isArray(item)) {
let temp = flatArray(item);
result.concat(temp);
} else {
result.push(item);
}
}
};
flatArray(arr);
// filter & sort asc
result = [...new Set(result)].sort((a, b) => a > b ? 1 : -1);
return result;
};
let result = autoFlatArray(arr);
log(`result all =`, result);
// result all = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
/*
const autoFlatArray = (arr = []) => {
let result = [];
const flatArray = (arr = []) => {
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (Array.isArray(item)) {
let temp = flatArray(item);
log(`temp`, temp);
result.concat(temp);
log(`result 1 =`, result);
} else {
result.push(item);
log(`result 2 =`, result);
}
}
};
flatArray(arr, []);
// filter & sort asc
result = [...new Set(result)].sort((a, b) => a > b ? 1 : -1);
return result;
};
*/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-13
*
* @description
* @augments
* @example
* @link
*
*/
let log = console.log;
var arr = [1, 2, 3, 4];
let result1 = arr.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
// is equivalent to
// let result2 = arr.reduce((acc, x) => acc.concat([x * 2]), []);
// [2, 4, 6, 8]
let result2 = arr1.reduce(
(acc, x) => {
log(`acc =`, acc);// []
log(`x =`, x);// 1
let temp = acc.concat([x * 2]);
log(`temp =`, temp);
return temp;
},
[],// initialValue
);
log(`flat array flatMap`, result1);
log(`flat array reduce`, result2);
/*
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
The reducer function takes four arguments:
Accumulator (acc)
Current Value (cur)
Current Index (idx)
Source Array (src)
*/
Array & flat
https://github.com/yygmind/blog/issues/43
https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/8#issuecomment-520795766
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
arr.flat(Infinity) === Infinity deep
let arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
// Array.from
let result = Array.from(new Set(arr.flat(Infinity))).sort((a,b)=> a > b ? 1: -1);
console.log(`flat array with unique filter & sort`, result);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
OR
let arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
let result = [...new Set(arr.flat(Infinity))].sort((a,b)=> a > b ? 1: -1);
console.log(`flat array with unique filter & sort`, result);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11349814.html
未经授权禁止转载,违者必究!