简体   繁体   English

如何创建javascript多维数组?

[英]How to create javascript multi dimensional array?

I want to create an multidimensional array this from following code. 我想从以下代码创建一个多维数组。

var i = 0;
$('.button_image').each(function () {
    buttons[i]['left'] = $(this).position().left;
    buttons[i]['top'] = $(this).position().top;
    i++;
});

Array should be like this 数组应该是这样的

buttons[1]['left']=10;
button[1][top]=20;
buttons[2]['left']=40;
button[2][top]=50;

but it gives following error on firefox console. 但它在firefox控制台上给出了以下错误。

TypeError: buttons[i] is undefined
buttons[i]['left']=$(this).position().left;

Please tell me what is wrong on my code. 请告诉我我的代码有什么问题。 Thanks in advance. 提前致谢。

I want this format: 我想要这种格式:

[rows] => Array ( 
        [0] => Array ( 
                 [column1] => hello 
                 [column2] => hola 
                 [column3] => bonjour )
        [1] => Array ( 
                 [column1] => goodbye 
                 [column2] => hasta luego 
                 [column3] => au revoir ) )

Javascript has no multidimensional Arrays, it only has Arrays of Arrays or likewise Objects of Objects, etc. Javascript没有多维数组,它只有数组数组或类似物体对象等。

So, to create an array inside another array, you need to define the element i of the initial array itself as an array first. 因此,要在另一个数组中创建一个数组,首先需要将初始数组本身的元素i定义为数组。 You can do so by simply adding an initialisation buttons[i] = []; 您只需添加初始化buttons[i] = []; inside your each loop. 在你的each循环内。

However, what you really need, is an object instead of an array, since arrays can only have numeric indices like buttons[0][2] , and objects can have arbitrary indices like buttons[0]['left'] or the equivalent notation buttons[0].left like you wrote in your question. 然而,你真正需要的是一个对象而不是一个数组,因为数组只能有像buttons[0][2]这样的数字索引,而对象可以有任意索引,如buttons[0]['left']或等价物符号buttons[0].left就像你在问题中写的一样。

// this is making buttons[i] a new object:
buttons[i] = {};  // the preferred notation over "new Object();"

// now this works without problem:
buttons[i]['left'] = $(this).position().left;
buttons[i]['top']  = $(this).position().top;

// equivalent notation:
buttons[i].left = $(this).position().left;

You should initialize the objects before using the second dimension of the array. 您应该在使用数组的第二个维度之前初始化对象。

Here's an example: 这是一个例子:

var myArr = new Array();
myArr[0] = new Array();
myArr[0][0] = 'Hello';
myArr[0][1] = 'World!';
alert(myArr[0][0] + ' ' + myArr[0][1]);

Simply push new objects into an array. 只需将新对象推入数组即可。

var buttons = [];
$('.button_image').each(function () {
  buttons.push({
    left: $(this).position().left,
    top: $(this).position().top
  });
});

Then you can access the objects using indexes: buttons[1].left for example. 然后,您可以使用索引访问对象: buttons[1].left例如。

You could do far simpler : 你可以做得更简单:

var buttons = $('.button_image').map(function () {
    return $(this).position();
}).get();

That said, here is how to fix your code : 也就是说,这是如何修复您的代码:

var i = 0, buttons = []; // init "buttons" as an array
$('.button_image').each(function () {
    buttons[i] = {}; // init "buttons[i]" as an object
    buttons[i]['left'] = $(this).position().left;
    buttons[i]['top'] = $(this).position().top;
    i++;
});

You might have guessed that it's actually not a multidimensional array, I would rather call it an "array of objects" : [{ left: 10, top: 20 }, { left: 40, top: 50 }] . 您可能已经猜到它实际上不是一个多维数组,我宁愿称之为“对象数组”: [{ left: 10, top: 20 }, { left: 40, top: 50 }] However, I don't see the connection between the first part of your question and the desired format. 但是,我没有看到问题的第一部分与所需格式之间的联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM