在javascript

时间:2017-10-03 09:01:34

标签: javascript arrays matrix multidimensional-array

我在javascript中定义了一个数组:

var chessboard = []; 
chessboard.push(["a1", "b1", "c1","d1","e1","f1","g1","h1"]);
chessboard.push(["a2", "b2", "c2","d2","e2", "f2","g2","h2"]);
chessboard.push(["a3", "b3", "c3","d3","e3", "f3","g3","h3"]);
chessboard.push(["a4", "b4", "c4","d4","e4", "f4","g4","h4"]);
chessboard.push(["a5", "b5", "c5","d5","e5", "f5","g5","h5"]);
chessboard.push(["a6", "b6", "c6","d6","e6", "f6","g6","h6"]);
chessboard.push(["a7", "b7", "c7","d7","e7", "f7","g7","h7"]);
chessboard.push(["a8", "b8", "c8","d8","e8", "f8","g8","h8"]);

我正在努力寻找的是如果元素被传递,如何找到索引。

示例:如果我传递“a5”,程序应该能够告诉我(行,列)为(4,0)

**CODE:**
<!DOCTYPE html>
<html>
<head>
<title>Javascript Matrix</title>
</head>
<body>
<script>
var chessboard = [];
chessboard.push(["a1", "b1", "c1","d1","e1", "f1","g1","h1"]);
chessboard.push(["a2", "b2", "c2","d2","e2", "f2","g2","h2"]);
chessboard.push(["a3", "b3", "c3","d3","e3", "f3","g3","h3"]);
chessboard.push(["a4", "b4", "c4","d4","e4", "f4","g4","h4"]);
chessboard.push(["a5", "b5", "c5","d5","e5", "f5","g5","h5"]);
chessboard.push(["a6", "b6", "c6","d6","e6", "f6","g6","h6"]);
chessboard.push(["a7", "b7", "c7","d7","e7", "f7","g7","h7"]);
chessboard.push(["a8", "b8", "c8","d8","e8", "f8","g8","h8"]);
alert(chessboard[0][1]); // b1
alert(chessboard[1][0]); // a2
alert(chessboard[3][3]); // d4
alert(chessboard[7][7]); // h8
</script>
</body>
</html>

这就是我现在所处的位置。

EDIT2:

非常感谢大家:)我感到非常高兴。

它似乎有多种方式! 我要做的是这个&gt;&gt; 找出两个正方形的(行,列)。 例: 广场1:a4
广场2:c7

||x,y|| = row1-row2, column1-column2

现在从另一个8x8矩阵/数组中找出(x,y)。 并显示矩阵(x,y)的数据。

5 个答案:

答案 0 :(得分:3)

因为它是一个棋盘,你可以从元素本身获取信息,而无需迭代棋盘:

&#13;
&#13;
function findCoo(el) {
  return [
    el[1] - 1, // the row value - 1
    el[0].codePointAt() - 'a'.codePointAt() // the column ascii value - ascii value of a
  ];
}

console.log("a5", findCoo("a5"));
console.log("d6", findCoo("d6"));
alert("a5" + ' ' + findCoo("a5"));
&#13;
&#13;
&#13;

答案 1 :(得分:2)

对于频繁使用,我建议使用一个具有位置的对象,并返回一个带有所需字段坐标的数组。

var chessboard = [["a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"], ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"], ["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"], ["a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"], ["a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"], ["a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"], ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"], ["a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"]],
    positions = Object.create(null); // empty object without prototypes

chessboard.forEach(function (a, i) {
    a.forEach(function (b, j) {
        positions[b] = [i, j];
    });
});

console.log(positions['a5']); // [4, 0]
console.log(positions);
.as-console-wrapper { max-height: 100% !important; top: 0; }

要获取字段名称,您可以使用Number#toString,字母为基数36

function getField(i, j) {
    return (j + 10).toString(36) + (i + 1).toString(10);
}

console.log(getField(4, 0)); // 'a5'
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

您可以尝试这样的事情:

逻辑:

  • 遍历所有数组select count(distinct nvl(hour,0)) from hours; 并遍历每一行。
  • 检查行中是否存在该值。
    • 如果是,则将行作为迭代器返回,将列作为索引

&#13;
&#13;
chessBoard
&#13;
&#13;
&#13;

答案 3 :(得分:1)

由于您要存储的是棋盘,因此您可以向chessboard添加方法,而不是遍历数组中的所有元素并进行搜索,并返回[row,column]简单计算:

&#13;
&#13;
let chessboard = [["a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"], ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"], ["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"], ["a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"], ["a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"], ["a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"], ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"], ["a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"]]

chessboard.findEl = (input) => ([input[1]-1 ,input[0].charCodeAt(0)-97])

console.log(chessboard.findEl("a5"))
console.log(chessboard.findEl("b4"))
&#13;
&#13;
&#13;

答案 4 :(得分:0)

试试这个

function getElement(val){
var result;
  for(var i=0;i<chessboard.length;i++){
         result=chessboard[i].indexOf(val);
     if(result!=-1){
        result='[' + i+',' + result + ']';
        break;
     }
  }
  return result;
}

console.log(getElement("a5"));