1

I want to reverse a multidimensional array and have the code below so far:

currentPositionPieceValues: [[], [], [], [], [], [], [], []]

gameDetails.currentPositionPieceValues
          .map(row => row.reverse())
          .reverse();

This seems to flip it horizontally, but not vertically, if you see what I mean.

How can I get it to do both?

4
  • 6
    Are you assigning the result to a variable? because .map is going to create a new array, not mutate the existing one (reverse, on the other hand, does mutate its array) Commented Aug 31, 2021 at 12:16
  • could you please share an example with sample data with it's expected output? Your code seems working fine for me. Commented Aug 31, 2021 at 12:17
  • @ChrisG That dupe target has a similar desired result, but not really the same problem. Nothing in it would explain to the asker why their very similar solution isn't working (E.g. it doesn't make any reference to the in-place modification of .reverse(), or the new array created by .map()) Commented Aug 31, 2021 at 12:26
  • @ChrisG - That's not a dupe. That's reversing the inner arrays only. This question wants both reversed. Commented Aug 31, 2021 at 12:26

2 Answers 2

1

You need to do:

gameDetails.currentPositionPieceValues = gameDetails.currentPositionPieceValues
          .map(row => row.reverse())
          .reverse();

This is because map creates a new array, so the outer array will get not get reversed unless you reassign it.

The inner arrays are being reversed because objects are always passed by reference in javascript.

Sign up to request clarification or add additional context in comments.

6 Comments

This is 100% a dupe
@ChrisG feel free to find it and mark it
@ChrisG - Why are telling me that instead of voting to close? There are no votes to close as of the time of writing this.
@ChrisG I have 5,514 close votes.... so sorry I spent 30 seconds and wrote up an answer instead of closing.... lol
It's not a 100% dupe, but it doesn't have to be. It shows the general way to do this and points out that you need to use the return value of map() which is a crucial piece of the answer. The other relevant dupe is stackoverflow.com/questions/42242257/…
|
0

Map returns a new array. So when you do the final reverse you are changing the new array, not currentPositionPieceValues.

const arr = gameDetails.currentPositionPieceValues;
arr.forEach(row => row.reverse());
arr.reverse();

3 Comments

This is 100% a dupe...
This isn't about your record, it's about not teaching people to post instead of google, i.e. clutter up the site with duplicate content. You're not only encouraging this, you're contributing. If this is how SO works now, fine. But I don't think it does, or should.
@ChrisG - That doesn't change the fact that the question you listed as a dupe of this one, was in fact not a dupe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.