有一个4x4板,总共有16个字母。在这个数组中,2,1表示第三行第二列中的字母。
const coordPairs = [
[ [2, 1], [4, 1] ]
[ [4, 0], [4, 1], [4, 2], [4, 3] ]
[ [0, 0], [0, 1], [0, 2] ],
[ [1, 0], [3, 0], [4, 0] ]
]试图找出如何将像2,1这样的一对连接到游戏板上的单个字母,这是一个由16个字符串(字母)组成的数组。
最终目标是该函数根据游戏板和你提供的坐标为单词生成字符串。
JSFiddle附带评论:https://jsfiddle.net/8euxzgy2/4/
发布于 2018-08-04 21:38:12
假设这是零索引平方矩阵。你可以取rows * coord[0] + coord[1]的号码
let str = "abcdefghijklonop"
let rows = 4
const coordPairs = [[0, 0], [2, 1], [3, 1] ];
/* a b c d
* e f g h
* i j k l
* m n o p */
letters = coordPairs.map(coord => str[coord[0]* rows + coord[1]])
console.log(letters)
https://stackoverflow.com/questions/51689701
复制相似问题