Merging the elements of nested arrays into one big array

Vladimir Fomene

I'm trying to merge the elements of the array into one big array. But I receive a message saying:

ReferenceError: reduce is not defined

Here is my code:

var arrays = [[1, 2, 3], [4, 5], [6]];

console.log(reduce(arrays, function(arrayOne, arrayTwo){
    return arrayOne.concat(arrayTwo);
}, 0));
Benoit Esnard

reduce() is a method of the Array object, so you must use arrays.reduce().

Moreover, since your initial value is set to 0 (the 2nd parameter), you can't use arrayOne.concat on it, since it's not an array, so you must set the initial value to [].

var arrays = [[1, 2, 3], [4, 5], [6]];

console.log(arrays.reduce(function(arrayOne, arrayTwo){
    return arrayOne.concat(arrayTwo);
}, []));

Note that calling Array.flat is easier:

var arrays = [[1, 2, 3], [4, 5], [6]];
// If you expect a multi-level nested array, you should increase the depth.
var depth = 1;

console.log(arrays.flat(depth));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I unmarshal nested XML elements into an array?

分類Dev

remove duplicate elements in proceeding arrays inside array of arrays

分類Dev

Merging sorted arrays of unequal length

分類Dev

Merging sorted arrays of unequal length

分類Dev

Merging Dataframes with Arrays?

分類Dev

lodash where function and getting nested array elements

分類Dev

Copying elements from one character array to another

分類Dev

Pyspark: merging values in a nested list

分類Dev

Efficiently create arrays from a next n elements from a array

分類Dev

Efficiently create arrays from a next n elements from an array

分類Dev

javascript: loop through object array and pushing elements into one array

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Pushing arrays of one variable in one array

分類Dev

Merging associative arrays

分類Dev

concatinate array of arrays with its reversed elements in place

分類Dev

How to write a nested array where the inner arrays are different lengths?

分類Dev

Merging points closest to each other in an array of 3 elements each

分類Dev

Merging nested dictionaries in Python

分類Dev

Consolidating an array of arrays into an object while merging values under a specific key (there are duplicates in the array)

分類Dev

Create nested hashes from an array of elements for JSON encoding

分類Dev

Merge matrices/2d-Arrays of arbitrary size into one big 2d array

分類Dev

How can be array elements multiplied with big O(n)

分類Dev

Nested ng-repeat with array of objects which contain arrays

分類Dev

Referencing array elements by strings, and initialising arrays in awk

分類Dev

How to ng-repeat over array with arrays as elements?

分類Dev

get an array of arrays with unique elements

分類Dev

RethinkDB: Create Index on field in nested array (running into big data scenario)

分類Dev

Need to form an array object with nested data out of 3 different arrays

分類Dev

Merging and reordering two arrays of objects

Related 関連記事

  1. 1

    How do I unmarshal nested XML elements into an array?

  2. 2

    remove duplicate elements in proceeding arrays inside array of arrays

  3. 3

    Merging sorted arrays of unequal length

  4. 4

    Merging sorted arrays of unequal length

  5. 5

    Merging Dataframes with Arrays?

  6. 6

    lodash where function and getting nested array elements

  7. 7

    Copying elements from one character array to another

  8. 8

    Pyspark: merging values in a nested list

  9. 9

    Efficiently create arrays from a next n elements from a array

  10. 10

    Efficiently create arrays from a next n elements from an array

  11. 11

    javascript: loop through object array and pushing elements into one array

  12. 12

    How to get the combination of array values from nested arrays in an array of objects

  13. 13

    Pushing arrays of one variable in one array

  14. 14

    Merging associative arrays

  15. 15

    concatinate array of arrays with its reversed elements in place

  16. 16

    How to write a nested array where the inner arrays are different lengths?

  17. 17

    Merging points closest to each other in an array of 3 elements each

  18. 18

    Merging nested dictionaries in Python

  19. 19

    Consolidating an array of arrays into an object while merging values under a specific key (there are duplicates in the array)

  20. 20

    Create nested hashes from an array of elements for JSON encoding

  21. 21

    Merge matrices/2d-Arrays of arbitrary size into one big 2d array

  22. 22

    How can be array elements multiplied with big O(n)

  23. 23

    Nested ng-repeat with array of objects which contain arrays

  24. 24

    Referencing array elements by strings, and initialising arrays in awk

  25. 25

    How to ng-repeat over array with arrays as elements?

  26. 26

    get an array of arrays with unique elements

  27. 27

    RethinkDB: Create Index on field in nested array (running into big data scenario)

  28. 28

    Need to form an array object with nested data out of 3 different arrays

  29. 29

    Merging and reordering two arrays of objects

ホットタグ

アーカイブ