JavaScript多维数组
这不是我要问的问题,但我意外地遇到了JavaScript数组的问题。我有PHP的背景,看过一些网站后,我一点也不聪明 我正在尝试创建一个多维数组JavaScript多维数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,这不是我要问的问题,但我意外地遇到了JavaScript数组的问题。我有PHP的背景,看过一些网站后,我一点也不聪明 我正在尝试创建一个多维数组 var photos = new Array; var a = 0; $("#photos img").each(function(i) { photos[a]["url"] = this.src; photos[a]["caption"] = this.alt; photos[a]["background"] = this.c
var photos = new Array;
var a = 0;
$("#photos img").each(function(i) {
photos[a]["url"] = this.src;
photos[a]["caption"] = this.alt;
photos[a]["background"] = this.css('background-color');
a++;
});
错误消息:照片[a]未定义。我该怎么做?谢谢。JavaScript没有多维数组,而是数组的数组,可以以类似的方式使用 您可能需要尝试以下操作:
var photos = [];
var a = 0;
$("#photos img").each(function(i) {
photos[a] = [];
photos[a]["url"] = this.src;
photos[a]["caption"] = this.alt;
photos[a]["background"] = this.css('background-color');
a++;
});
请注意,您可以使用new Array()
而不是[]
,但通常建议使用后者。还要注意,第一行中缺少new Array()
的括号
更新:根据下面的注释,在上面的示例中,不需要使用数组的数组。一组对象更合适。代码仍然有效,因为数组是这种语言中的对象,但下面的代码会更好:
photos[a] = {};
photos[a]["url"] = this.src;
photos[a]["caption"] = this.alt;
photos[a]["background"] = this.css('background-color');
多维数组位于以下结构中:
var photos = new Array();
photos[0] = new Array();
将其用作:
photos[0][0] = this.src;
更多关于JS数组的信息。道格拉斯·克罗克福德(Douglas Crockford)会让您以这种方式创建它(“使用数组文字符号[]”):
现在请记住,您想要创建多维数组,这意味着数组中的数组。这意味着您需要创建内部阵列:
$("#photos img").each(function(i) {
photos[a] = []
//continue
我希望这有帮助 您正试图为照片[a][“url”],
照片[a][“caption”]
等分配内容,但照片[a]尚不存在photos
一开始是一个空数组,因此必须先将photos[a]
设置为某个值。因为您希望使用字符串键(“url”
,“caption”
,等等),所以这应该是一个普通对象(相当于php关联数组的javascript)(或者是一个哈希,如果您的代码库允许的话)。然后,您可以使用文字对象构造来简化您的函数,并使用Array#push
来消除不必要的a
:
var photos = [];
$("#photos img").each(function(img) {
photos.push({
url: img.src,
caption: img.alt,
background: img.style.backgroundColor
});
});
另外,确保这个
实际上是您的img
元素。一些每个
实现将此
设置为您案例中的全局对象
编辑:好的,它看起来像是jQuery。每个自动将this
设置为迭代元素,但不使用jQuery goodness包装,因此您必须在$()
中包装this
,或者使用普通DOM(我在示例中使用了后者)
edit2:无论如何,使用这个
有点奇怪,因为传递给每个
的回调函数都会收到一个参数。不妨使用此参数(重命名)。当我阅读此线程时,我试图了解多维关联数组。不清楚,所以我一直在研究,这就是我想到的:
var images = new Array;
images['logo'] = { 'src': 'http://example.com/image1.png', 'caption': 'this is the logo', 'background': '#000'};
images['background'] = { 'src': 'http://example.com/image2.png', 'caption': 'this is the background', 'background': '#FFF'};
alert(images['logo']['src']); /* output: http://example.com/image1.png */
alert(images['background']['caption']); /* output: this is the background */
希望这有帮助 数组包含数字键,而不是字符串。@Alsciende:该代码有效。数组在JavaScript中是一个对象,而对象和哈希表在这种语言中是相同的。因此,photos[0][“url”]
正在为photos[0]
分配一个url
属性。该属性有效,但不正确。在这种情况下使用数组是没有意义的,它只是让新手感到困惑代码>你是对的正确方法是照片[a]={}而不是[]
,因为var x=[];x['foo']='bar';alert(x.length)//0
您可以看到您没有填充数组,而是填充了对象属性。this.css
不起作用,您必须将this
包装在jQuery对象中:$(this.css
。虽然标题表示重复,但询问者真正想要的是一个对象数组,不是数组数组。此
的唯一问题是它没有css
方法。它必须首先包装在jQuery对象中。我对jQuery不太了解。我将使用你的建议。+1在可能的情况下使用纯js编码,并使其更具可读性。
Array(
[0] => Array(
"src" => "logo.png",
"alt" => "My Logo!",
"background" => "#ffffff"
)
)
var photos = [];
$("#photos img").each(function(img) {
photos.push({
url: img.src,
caption: img.alt,
background: img.style.backgroundColor
});
});
var images = new Array;
images['logo'] = { 'src': 'http://example.com/image1.png', 'caption': 'this is the logo', 'background': '#000'};
images['background'] = { 'src': 'http://example.com/image2.png', 'caption': 'this is the background', 'background': '#FFF'};
alert(images['logo']['src']); /* output: http://example.com/image1.png */
alert(images['background']['caption']); /* output: this is the background */