-
-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array.prototype.flat #9
Comments
const arr = [1, [2, 3, [4, 5]], 1, 2, [6, 7]]
Array.prototype.falt = function () {
return this.reduce((pre, cur) => {
cur instanceof Array ? pre.push(...cur.falt()) : pre.push(cur)
return pre
}, [])
}
console.log(arr.falt()); |
|
const arr = [1, [2, [3, [4, [5, [6, [7, [8, [9], 10], 11], 12], 13], 14], 15], 16]]
Array.prototype.flat = function (deep = 1) {
let res = []
if (deep === 'Infinity') {
this.forEach((item) => {
if (Array.isArray(item)) {
res = res.concat(item.flat());
} else {
res.push(item)
}
})
} else {
deep--
this.forEach(item => {
if (Array.isArray(item) && deep >= 0) {
res = res.concat(item.flat(deep))
} else {
res.push(item)
}
})
return res
}
}
console.log('展开一层', arr.flat(1))
console.log('完全展开', arr.flat(Infinity)) |
Array.prototype._flat = function (depth = Infinity) { |
Array.prototype._flat = function (deep = 1) {
if(deep === 0) return this;
let res = [];
for (const item of this) {
if (Array.isArray(item)) {
res = res.concat(item._flat(--deep));
} else {
res.push(item);
}
}
return res;
};
const arr = [12, 2, 4, [1, 3, [2, 34,[34,5]]]];
console.log(arr._flat(0));
console.log(arr._flat(1));
console.log(arr._flat(2));
console.log(arr._flat(Infinity)); |
const arr = [1, [2, 3, [4, 5]], 1, 2, [6, 7]]
Array.prototype._flat = function (deep = 1) {
let res = []
deep--
for (const p of this) {
if (Array.isArray(p) && deep >= 0) {
res = res.concat(p._flat(deep))
} else {
res.push(p)
}
}
}
const val = [1, 2, [3, 4, [5, 6]]] .flat()
console.log(val); |
1. 递归解法递归的思路很简单,遍历数组,如果数组内部出现数组,进行递归操作, /**
* @description: 递归解决 数组扁平化flat
* @param {*} nums
* @param {*} deepth
* @return {*}
*/
function flat1(nums, deepth = 1){
// 如果是 无穷大,默认20层
if(deepth === Infinity){
deepth = 20;
}
// 校验参数
if(deepth <= 0){
return nums;
}
const result = [];
function flats(arr, deep){
arr.forEach(element => {
// 控制递归条件
if(Array.isArray(element) && deep > 0){
flats(element, deep - 1);
}else{
result.push(element)
}
});
}
flats(nums,deepth)
return result;
} 2. 迭代解法递归转迭代一般就比较难思考,这里我们可以引入一个队列的概念,栈也可以( /**
* @description: 迭代 解决数组扁平化flat
* @param {*} nums
* @param {*} deepth
* @return {*}
*/
function flat2(nums, deepth = 1){
// 如果是 无穷大,默认20层
if(deepth === Infinity){
deepth = 20;
}
// 校验参数
if(deepth <= 0){
return nums;
}
// 用队列层数的思想代替递归
const queue = [...nums];
let deep = 0;
while(deep <= deepth){
deep += 1;
for(let i = 0, n = queue.length;i < n; i ++) {
const top = queue.shift(); // 取出队头元素
// 如果队头元素是数组
if(Array.isArray(top) && deep <= deepth){
// 展开后进行入队
queue.push(...top);
}else{
// 如果不是array, 直接入队
queue.push(top)
}
}
}
return queue;
} |
|
reduce方法
|
// Array.prototype.flat() 扁平化数组,参数代表展开的层数(默认1)
const arr = [1,2,[3,[4]]];
arr.flat(Infinity) // [1,2,3,4]
/**
* 无副作用(不会改变原数组)
* @param {*} depth
*/
Array.prototype._flat = function (depth = 1) {
return this.reduce((acc, cur) => { // 前一个值,当前值
cur instanceof Array ? (function() { // 只能是表达式
if(depth-- > 0) acc.push(...cur._flat(depth))
else acc.push(cur)
})() : acc.push(cur);
return acc;
}, [])
}
console.log(arr._flat(Infinity)) |
function flatten(arr, deep) {
while (arr.some(item => Array.isArray(item)) && deep) {
arr = [].concat(...arr);
deep--;
}
return arr;
} |
|
Array.prototype.myFlat = function (deep = Infinity) {
let res = [];
deep--;
this.forEach((item) => {
if (Array.isArray(item) && deep >= 0) {
res = res.concat(item.myFlat(deep));
} else {
res.push(item);
}
});
return res;
}; |
Array.prototype.myFlat = function(depth = 1) {
if (depth < 1) {
return this.slice();
}
return this.reduce((acc, val) => {
if (Array.isArray(val)) {
acc.push(...val.myFlat(depth - 1));
} else {
acc.push(val);
}
return acc;
}, []);
}; |
|
const arr = [1, [2, 3, [4, 5]], 1, 2, [6, 7]]
Array.prototype.flat = function (deep = 1) {
let result = []
for (let item of this) {
if (Array.isArray(item) && deep >= 1) {
result = result.concat(item.flat(--deep))
} else {
result.push(item)
}
}
return result
}
console.log(arr.flat(2)); |
/**
* @deep 表示只保证deep + 1层内进行数组扁平化
* const arr = [1, [2, [3]]];
* console.log(arr.flat(1));
*/
const arr = [1, [2, 3, [4, 5]], 1, 2, [6, 7]]
Array.prototype.flat = function (deep = 1) {
//deep表示递归了到的层数,可以想象成deep是嵌套层的下标
deep--;
const res = []
for (let p of this) {
if (Array.isArray(p) && deep >= 0) {
//res.concat(p.flat(deep)); concat不会修改原来的值 error
res = res.concat(p.flat(deep));
} else {
res.push(p);
}
}
return res;
}
console.log(arr.flat(1)); |
function flatten(input, shallow, strict, output) { |
Array.prototype.flat = function (deep = 1) { |
|
const flat = arr => {
const res = []
const dfs = list => {
for (const item of list) {
if (Array.isArray(item)) {
dfs(item)
}else {
res.push(item)
}
}
}
dfs(arr)
return res
} |
Array.prototype.myFlat = function (deep = 1) {
let res = []
deep--
for (const item of this) {
if (Array.isArray(item) && deep >= 0) {
res.push(...item.myFlat(deep))
} else {
res.push(item)
}
}
return res
} |
|
The text was updated successfully, but these errors were encountered: