具有名称的JavaScript字典
问题描述:
我想在javascript中创建一个字典,如下所示:
I want to create a dictionary in javascript like the following:
myMappings = [
{ "Name":10%},
{ "Phone":10%},
{ "Address":50%},
{ "Zip":10%},
{ "Comments":20%}
]
我想稍后填写一个html表,将表的标题设置为myMappings的第一列,将列的宽度设置为第二列。因为我是JS的新手,找不到干净的方式。可以一些建议吗?
I want to populate an html table later and want to set the titles of table to the first column of myMappings and the width of columns to the second. As I am new to JS, having trouble finding the clean way. Can some pls suggest?
谢谢
答
主要问题我看看你有什么是很难循环,填充一个表。
The main problem I see with what you have is that it's difficult to loop through, for populating a table.
只需使用数组:
var myMappings = [
["Name", "10%"], // Note the quotes around "10%"
["Phone", "10%"],
// etc..
];
...这简化了访问:
... which simplifies access:
myMappings[0][0]; // column name
myMappings[0][1]; // column width
或者:
Alternatively:
var myMappings = {
names: ["Name", "Phone", etc...],
widths: ["10%", "10%", etc...]
};
并访问:
myMappings.names[0];
myMappings.widths[0];