Enk*_*idu 5 javascript arrays multidimensional-array data-structures
我有一个 10 行 10 列的表格。我想定义一个数组,我可以在其中放置一个值,例如 pos。第 5 行,第 3 列。
值本身是一个包含更多条目的数组。而这个数组的入口也是一个数组。
例子:
Row 1, column 1:
My text 1, Link to text 1
My text 2, Link to text 2
Row 4, column 5:
My text 3, Link to text 3
Row 6, column 2:
My text 1, Link to text 1
My text 2, Link to text 2
My text 3, Link to text 3
My text 4, Link to text 4
Run Code Online (Sandbox Code Playgroud)
并非每个表条目都需要定义。一个表元素条目可以有多个条目。一个条目由两个值组成。一个文本和文本的链接。
html-table 已经定义。现在我想用上面的值(链接)填充它。
我的问题是,如何创建一个有效的数据结构,以便我可以轻松地找到具有条目的表位置(也许不需要循环 10 行 10 列)。对于每个条目,我想获取文本 + 链接列表。
以及如何访问/阅读我定义的每个条目。(我可以将值放置到我的 html 表中。)
如果有人能给我一些如何设置这样一个数据结构的代码示例,我将不胜感激。
如果内存不是问题,就使用数组的数组;
var table = [];
table.length = 10; // 10 rows;
for (var i = 0; i < array.length; i++) {
table[i] = [];
table[i].length = 20; // 20 columns for each row.
}
Run Code Online (Sandbox Code Playgroud)
如果表很大但只使用了几个单元格,您还可以使用散列的散列:
var table = {};
table.rowCount = 10; // there're 10 rows
table[1] = {}
table[1].columnCount = 20 // 20 cells for row 1
table[1][3] = "hello world";
// visit all cells
for (var row in table) {
for (var column in table[row] {
console.log(table[row][column]);
}
}
Run Code Online (Sandbox Code Playgroud)
您甚至可以混合哈希和数组。