gpt4 book ai didi

javascript - 在 jQuery 中使用数组或对象?

转载 作者:行者123 更新时间:2023-11-30 10:06:10 25 4
gpt4 key购买 nike

我知道 JavaScript 不支持关联数组。我们将创建一个以字符串形式的键的对象。

这里是 https://jsfiddle.net/maheshwaghmare/sgw07L8L/6/

E.g.  "hometitle":[
["fontsize","12px"],
["lineheight","16px"],
],

但是,我需要像这样创建它:

E.g.  ".home-title":[
["font-size","12px"],
["line-height","16px"],
],

那么,在这种情况下,我该如何存储和检索值而不在同一父级中存储重复值。

E.g. (.home-title) doesn't contain another (font-size)

注意: parent (.home-title)可能是- (.home-title .headings)- (#header .title) 等

我知道我们不能将这种键存储在对象中。

任何解决方案..!提前致谢...!

最佳答案

要轻松访问属性(并避免重复的键),您可以使用对象而不是数组的数组。

没有什么可以阻止您使用这种字符串作为对象中的键。

例子:

var obj = {
".home-title": {
"font-size": "12px",
"line-height": "16px"
},
".home-title .headings": {
"font-size": "16px",
"line-height": "20px"
},
"#header .title": {
"font-size": "12px",
"font-weight": "bold"
}
};

关于javascript - 在 jQuery 中使用数组或对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29053894/

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