如何在 JavaScript 中访问数组中的数据
var lotsData = [
{
index: 0,
data: 'I want to be in HTML',
},
{
index: 1,
data: 'I dont' want to be in HTML',
}]
可以说我更喜欢一个带有数组的变量。如何访问索引 0,假设它连接到单击事件并且我想使用数据:
$('.tab').live('click', function() {
console.log("I was clicked");
$('#fancy').text(WHAT GOES HERE TO ACCESS data from index 0?);
});
var lotsData = [
{
index: 0,
data: 'I want to be in HTML',
},
{
index: 1,
data: 'I dont' want to be in HTML',
}]
Lets say I prefer to have one var with an array. How do I access the index 0, say its connected to a click event and I want to use data:
$('.tab').live('click', function() {
console.log("I was clicked");
$('#fancy').text(WHAT GOES HERE TO ACCESS data from index 0?);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery 是用 Javascript 编写的,Javascript 本身提供了 Array 对象。
因此,访问数组的第 0 个元素是
array_name[0]
在您的示例中,您将对象存储为数组的元素。您的对象包含“index”属性,但是请注意,您的“index”属性与数组中的元素索引无关。您不应该包含“索引”属性...例如:
更好的示例是:
添加:尾随逗号
请注意,虽然 Javascript 是一种功能强大的语言,但它确实有其怪癖。
一个重要的就是尾随逗号,例如
问题是尾随逗号不是 Javascript 语言的正式部分。大多数 JS 引擎都支持它,但有一个非常重要的引擎不支持:Internet Explorer 浏览器不支持尾随逗号,遇到逗号就会悲惨地死去。
由于 IE 的独特工作方式,您的测试应始终包括 IE。
我在 IE 7 下测试。
jQuery is written in Javascript, and Javascript itself provides the Array object.
So accessing the 0th element of an array is
array_name[0]
In your example, you're storing objects as the elements of the array. Your objects are including an "index" property, but beware, your "index" property has nothing to do with the element index in the array. You should NOT include an "index" property... Eg:
The better example would be:
Added: trailing commas
Note that while Javascript is a powerful language, it does have its quirks.
An important one is trailing commas, such as
The problem is that the trailing comma is not a formal part of the Javascript language. Most JS engines support it, but a very important one does not: the Internet Explorer browser does not support trailing commas and will die a sad death when it encounters one.
Your testing should always include IE due to its unique ways of doing things.
I test in IE 7.
当然,这是假设您的索引顺序正确(因为它们似乎来自您的示例数据)。
在这种情况下,没有理由保留数组对象的
index
属性。使用简单的字符串数组会更容易使用。例如
,您只需引用
lotsData[0]
of course this is assuming your indexes are in correct order (as they seem to be from your example data).
And in that case there is no reason to keep a
index
property of your array'd objects. using a simple string array would be much easier to work with.example
this way you would just have to simply reference
lotsData[0]
首先不要使用 .live() 除非你真的需要( http://jupiterjs.com/news/why-you-should-never-use-jquery-live )
但是如果你查看 .live() 文档( http://api.jquery.com/live/ )您将看到您可以将数据传递给.live(),它将在回调中可用。
那么:
这就是您要问的吗?
或者您是否正在寻找一种方法来迭代lotsdata并找出数组中的哪个项目具有.index = 0?
First off don't use .live() unless you really really need to ( http://jupiterjs.com/news/why-you-should-never-use-jquery-live )
But if you look at the .live() documentation ( http://api.jquery.com/live/ ) you will see the fact that you can pass data to .live(), that will be available in the callback.
So:
Is that what you are asking?
Or are you looking at a a way to iterate through lotsdata and find out which item in the array has .index = 0?
你可以尝试这个,不是最好的解决方案:
You can try this, not best solutuion: