Skip to content
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

[js] 第119天 写一个方法把多维数组降维 #1059

Open
haizhilin2013 opened this issue Aug 12, 2019 · 11 comments
Open

[js] 第119天 写一个方法把多维数组降维 #1059

haizhilin2013 opened this issue Aug 12, 2019 · 11 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第119天 写一个方法把多维数组降维

@haizhilin2013 haizhilin2013 added the js JavaScript label Aug 12, 2019
@fengyun2
Copy link

const arr = [1, 2, [3, 4, [5, 6]]]
arr.flat(Infinity)
// [1, 2, 3, 4, 5, 6]

@NicholasBaiYa
Copy link

function flattenDeep(arr) {
   return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}

抄MDN上的,会把空项过滤。

@nowherebutup
Copy link

  function handlerArr(arr, _rst) {
    const rst = _rst || [];
    arr.forEach(ele => {
      typeof ele === 'object' ? handlerArr(ele, rst) : rst.push(ele);
    });
    return rst;
  }

@LinStan
Copy link

LinStan commented Aug 13, 2019

let arr = [1, 2, {}, 3, [4, {}, 5], [6, 7, [8, null, 9]]];
//递归
function flat (arr) {
  let ret = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] instanceof (Array)) {
      ret = ret.concat(flat(arr[i]))
    }
    else {
      ret.push(arr[i])
    }
  }
  return ret
}
console.log(flat(arr))
//ES10新增的内置方法
arr.flat(Infinity)

@HuoXiaoYe
Copy link

		function flatArr(arr){
			var result = [];
			arr.forEach((item)=>{
				if(Array.isArray(item)){
					let subResult = flatArr(item);
					result = result.concat(subResult);
				}else{
					result[result.length] = item;
				}
			})
			return result;
		}
		console.log(flatArr([1, 2, [3, 4, [5, 6]]])) // [1, 2, 3, 4, 5, 6]

@ghost
Copy link

ghost commented Aug 13, 2019

function flatten(arr, depth = 1) {
    if (!depth || !Array.isArray(arr)) return arr
    return arr.reduce((prev, mem) => 
        prev.concat(flatten(mem, depth - 1)), [])
}

怎么感觉和搬mdn的那位这么像...但是这个支持指定深度。

@LJH520
Copy link

LJH520 commented Aug 13, 2019

@Konata9
Copy link

Konata9 commented Aug 13, 2019

const flatArr = (arr, deep = false) =>
  arr.reduce((prev, cur) => {
    if (deep && Array.isArray(cur)) {
      return prev.concat(flatArr(cur, deep));
    } else {
      return prev.concat(cur);
    }
  }, []);

const arr = [1, [2, [3, [4, [5], 6], 7], 8], 9];
const arr2 = [{}, [{}, {}, {}, [2, 3, 4, [5, 6, 7, [{}, {}]]]]];
console.log(flatArr(arr, true));

@Jingce-lu
Copy link

const flatArr = (arr) => Array.isArray(arr)
  ? arr.reduce( (a, b) => [...a, ...flatArr(b)] , [])
  : [arr]

flatArr([1, [[2], [3, [4]], 5]])

@zhaofeipeter
Copy link

zhaofeipeter commented Jul 30, 2020

const flatArray = arr => arr.flat(Infinity);

@xiaoqiangz
Copy link

const arr = [2,3,[4,5,6],[7,9,0],10,[344,666,[888,999]]]
function flat(data) {
return data.reduce((prev,cur) => {
if (Array.isArray(cur)) {
return prev.concat(flat(cur))
} else {
return prev.concat(cur)
}
},[])
}
let arr1 = flat(arr)
console.log(arr1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests