具有名称的JavaScript字典 [英] JavaScript dictionary with names

查看:114
本文介绍了具有名称的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];

这篇关于具有名称的JavaScript字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆