gpt4 book ai didi

javascript - 将属性分配给数组 js/jquery 中的值

转载 作者:行者123 更新时间:2023-11-28 19:51:40 26 4
gpt4 key购买 nike

在我的脚本中,函数使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com