简体   繁体   English

JS如何展平嵌套数组

[英]JS how to flatten a nested array

The question I have been given is; 我得到的问题是;

Given a nested array or arrays, return a new, flat array with all the elements of all the nested arrays in their original order 给定嵌套数组或数组,返回一个新的平面数组,其中包含所有嵌套数组的所有元素的原始顺序

The answer that I have come up with is; 我想出的答案是;

function flattenArray (arrayOfArrays) {  
arrays = arrays.reduce(function(a, b){
    return a.concat(b);
}, []);

console.log(merged);

My answer is being tested against this; 我的答案正在接受考验;

describe("flattenArray", () => {
it("returns a flat array with all the elements of the nested arrays in      their original order", () => {
  let arrayOfArrays, expected;
  arrayOfArrays = [[1, 2], [], [3], ["hello", true]];
  expected = [1, 2, 3, "hello", true];
  expect(flattenArray(arrayOfArrays)).to.eql(expected);

  arrayOfArrays = [[1], [2], [[3, 4]]];
  expected = [1, 2, [3, 4]];
  expect(flattenArray(arrayOfArrays)).to.eql(expected);
});
it("does not mutate the passed array, i.e. returns a new array, leaving the original untouched", () => {
  const original = [[1, 2], [], [3, 4]];
  const flat = flattenArray(original);
  expect(original).to.not.equal(flat);
  expect(original).to.eql([[1, 2], [], [3, 4]]);
});
});

I have no clue how else to try and solve this question, does anyone have any sugestions. 我不知道如何尝试和解决这个问题,有没有人有任何sugestions。

You need to return the reduced array by taking the handed over array arrayOfArrays . 您需要通过移交数组arrayOfArrays返回简化数组。

function flattenArray(arrayOfArrays) {  
    return arrayOfArrays.reduce(function(a, b) {
        return a.concat(b);
    }, []);
}

For multiple nested arrays, you need to check for array and use a recursion of the function. 对于多个嵌套数组,您需要检查数组并使用函数的递归。

 function deepFlattenArray(arrayOfArrays) { return arrayOfArrays.reduce(function(a, b) { return a.concat(Array.isArray(b) ? deepFlattenArray(b) : b); }, []); } function flattenArray(arrayOfArrays) { return arrayOfArrays.reduce(function(a, b) { return a.concat(b); }, []); } console.log(deepFlattenArray([[[1, 2], [3, [4, 5], 6], 7], 8])); console.log(flattenArray([[[1, 2], [3, [4, 5], 6], 7], 8])); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Perhaps not the most elegant solution, but this will flatten any number of nested arrays. 也许不是最优雅的解决方案,但这会使任何数量的嵌套数组变平。 It's a recursive function that modifies a new array as a side-effect: 它是一个递归函数,将新数组修改为副作用:

 var arrOfArrs = [[1, 2, 3], [4, 5, 6], 7, [8, [9, 10, 11, 12]]]; newArr = []; function flattenArray(arr) { for(var i=0; i < arr.length; i++) { typeof arr[i] == 'object' ? flattenArray(arr[i]) : newArr.push(arr[i]); } } flattenArray(arrOfArrs); console.log(newArr); 

You can use reduce plus spread operator. 您可以使用reduce plus spread运算符。 Just coded this for the particular question but seems to work ok. 只是为特定问题编码,但似乎工作正常。 It uses recursion and works for Multiple nested arrays. 它使用递归并适用于多个嵌套数组。

 function flatArray(a){ return a.reduce( (accumulator, current) => { if (!Array.isArray(current)) return [...accumulator, current]; return flatArray([...accumulator, ...current]); }, []); } let a = [ 1, [2], [3,4], [], [5], [6, [7]], [[[8]]] ]; console.log(flatArray(a)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM