concatinate array of arrays with its reversed elements in place

Albert

I have an array of numbers that looks like this:

const arrayOfArrays: number[][] = [[1, 2], [1, 3]];

What I need to get is [[1, 2], [2, 1], [1, 3], [3, 1]].

Apparently this can be achieved in the following way:

// create an empty array
let result: number[][] = [];

for (const elArr of arrayOfArrays) {
    result.push(elArr);
    result.push(elArr.reverse());
}

But there must be some more elegant ways to do this?

Nina Scholz

You could use Array#flatMap and get the result in a single loop.

var array = [[1, 2], [1, 3]],
    result = array.flatMap(([...a]) => [[...a], a.reverse()]);

console.log(result);

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

get an array of arrays with unique elements

分類Dev

text in div appears in correct place but its design elements don't

分類Dev

Merging the elements of nested arrays into one big array

分類Dev

Referencing array elements by strings, and initialising arrays in awk

分類Dev

remove duplicate elements in proceeding arrays inside array of arrays

分類Dev

How can I repeat elements in place in an array in Python?

分類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

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

分類Dev

Place two form elements inline

分類Dev

Angular - Place scripts in its own import file

分類Dev

From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X

分類Dev

Broadcasting error when forming numpy array with elements as two other numpy arrays

分類Dev

Create a 2D array from paired elements of two 1D arrays in JavaScript

分類Dev

How to query mongo to find arrays that contain any of the elements from another array?

分類Dev

correct syntax for php switch case statement when using arrays and adding elements to a different array

分類Dev

merge array with array of arrays

分類Dev

Selection Sort has a few elements out of place

分類Dev

Reshape c# array in place

分類Dev

Comparing equality of elements in two arrays

分類Dev

Trying to find smallest array elements from 2 arrays returns 0 (doesn't work) and the other works. (C lang)

分類Dev

How to compare an array to an array of arrays?

分類Dev

Convert array of objects to array of arrays

分類Dev

How to insert an array into an array of arrays?

分類Dev

rearranging an array of arrays (python)

分類Dev

numpy fill an array with arrays

分類Dev

How to keep image in its original place while rotating

分類Dev

Finding elements of an array that match elements of a second array

分類Dev

Mapping elements of a matrix to its position in its spiral order traversal

Related 関連記事

  1. 1

    get an array of arrays with unique elements

  2. 2

    text in div appears in correct place but its design elements don't

  3. 3

    Merging the elements of nested arrays into one big array

  4. 4

    Referencing array elements by strings, and initialising arrays in awk

  5. 5

    remove duplicate elements in proceeding arrays inside array of arrays

  6. 6

    How can I repeat elements in place in an array in Python?

  7. 7

    Efficiently create arrays from a next n elements from a array

  8. 8

    Efficiently create arrays from a next n elements from an array

  9. 9

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

  10. 10

    Place two form elements inline

  11. 11

    Angular - Place scripts in its own import file

  12. 12

    From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X

  13. 13

    Broadcasting error when forming numpy array with elements as two other numpy arrays

  14. 14

    Create a 2D array from paired elements of two 1D arrays in JavaScript

  15. 15

    How to query mongo to find arrays that contain any of the elements from another array?

  16. 16

    correct syntax for php switch case statement when using arrays and adding elements to a different array

  17. 17

    merge array with array of arrays

  18. 18

    Selection Sort has a few elements out of place

  19. 19

    Reshape c# array in place

  20. 20

    Comparing equality of elements in two arrays

  21. 21

    Trying to find smallest array elements from 2 arrays returns 0 (doesn't work) and the other works. (C lang)

  22. 22

    How to compare an array to an array of arrays?

  23. 23

    Convert array of objects to array of arrays

  24. 24

    How to insert an array into an array of arrays?

  25. 25

    rearranging an array of arrays (python)

  26. 26

    numpy fill an array with arrays

  27. 27

    How to keep image in its original place while rotating

  28. 28

    Finding elements of an array that match elements of a second array

  29. 29

    Mapping elements of a matrix to its position in its spiral order traversal

ホットタグ

アーカイブ