在我的脚本中,函数使用 teststim 数组中的值。
var teststim = ["A", "B", "C"]
我想为这些值赋予“属性”,例如 A 具有属性“姓名”,B 具有“生日”属性,...
我需要找到一种方法来访问这些属性。我想到了这样的事情:
var teststim = {content: "A", attribute: "name"},
{content: "B", attribute: "birthday"},
{content: "C", attribute: "whatever"}
也许我比我想象的更接近,但我无法访问与内容值相对应的“属性”值。我缺少什么?
最佳答案
您需要一个对象数组:
var teststim = [{content: "A", attribute: "name"},
{content: "B", attribute: "birthday"},
{content: "C", attribute: "whatever"}];
for (var i=0; i<teststim.length; i++) {
var obj = teststim[i];
if (obj.content=='C') {
alert(obj.attribute); // "whatever"
};
};
关于javascript - 将属性分配给数组 js/jquery 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23347761/