jQuery 通过索引获取数组的值
使用 jQuery,我尝试使用索引获取 fileTypes
的值。我不知道如何执行此操作,但到目前为止我已经尝试过 settings.fileTypes[0]
但不起作用。任何帮助表示赞赏,谢谢!
var defaults = {
fileTypes : {
windows: 'Setup_File.exe',
mac: 'Setup_File.dmg',
linux: 'Setup_File.tar.gz',
iphone: 'iPhone App'
}
},
settings = $.extend({}, defaults, options);
using jQuery, I'm trying to get values of fileTypes
by using an index. I'm not sure how to do go about doing this but so far I've tried settings.fileTypes[0]
which does not work. Any help is appreciated, thanks!
var defaults = {
fileTypes : {
windows: 'Setup_File.exe',
mac: 'Setup_File.dmg',
linux: 'Setup_File.tar.gz',
iphone: 'iPhone App'
}
},
settings = $.extend({}, defaults, options);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
fileTypes
是一个对象,因此无法通过索引号访问其属性。对象不保证任何定义的顺序,因此如果您需要按索引顺序维护项目,则需要使用数组。
要获取给定示例的第一个项目,您可以按名称执行此操作:
要存储为可以通过索引检索其项目的数组,请尝试以下操作:
或者您可以执行对象数组,但我不认为这就是您想要的正在追寻:
fileTypes
is an Object, and as such, its properties can not be access via index number.Objects do not guarantee any defined order, so if you need to maintain items in an indexed order, you would need to use an Array instead.
To get the first item given your example, you would do so by name:
To store as an Array whose items you can retrieve by index, try this:
Or you could do an Array of Objects, though I don't think that's what you're after:
如果您的应用程序设计允许,也许您可以使用解决方法。
有时它对我有用。
通过这种方式,您可以通过索引(按特定顺序)访问第一级元素,如果您需要示例中的键,它们仍然存在。
Perhaps you can use a workaround if your application design allows it.
It worked for me some times.
On that way you can access the first level elements by index (in that specific order) and if you need the keys from your example, they are still there.