Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

javascript - 如何使用array-move方法更改数组中的位置?(How to change position in array using array-move method?)

My intention is to create PuzzleGame , example http://migo.sixbit.org/puzzles/fifteen/(我的意图是创建PuzzleGame ,例如http://migo.sixbit.org/puzzles/fifteen/)

Main priority is to move Cells from one with value , to another one undefined .(主要优先事项是将单元格从具有值的一个移到另一个未定义的 。) Here is my function:(这是我的功能:) moveCell(row, col) { this.puzzleTable = []; let actRow = 0, actCol = 0; for (let r = -1; r <= 1; r++) { actRow = row + r; if (actRow >= 0 && actRow < this.rowCount) { moveCells(this.puzzleTable[actRow][col], row, actRow); } } for (let c = -1; c <= 1; c++) { actCol = col + c; if (actCol >= 0 && actCol < this.colCount) { moveCells(this.puzzleTable[row][actCol], col, actCol); } } } For example:(例如:) Arr[0][0] to Arr[1][1] I do not want to change their value , I want them swap(我不想更改其 ,我希望它们交换) What is the best practice ?(最佳做法是什么?)   ask by Rejzozroutko Tezbir translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well you could simply store the values of both cells in two temporary variables and assign them to each other accordingly.(好吧,您可以简单地将两个单元格的值存储在两个临时变量中,并相应地相互分配它们。)

For example:(例如:) var tempA=Arr[0][0]; var tempB=Arr[1][1]; Arr[0][0]=tempB; Arr[1][1]=tempA;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...