简体   繁体   English

JavaScript二维数组

[英]JavaScript Two dimensional Array

I am creating javascript two dimensional array 我正在创建javascript二维数组

code is : 代码是:

var field_arr=[];

            $(".dr").each(function(index){

            Id=$(this).attr("id");
            alert(dragId);
            topPos=$("#"+ dragId).position().top;
            left=$("#"+ dragId).position().left;
            parentDiv=$("#"+dragId).parent().attr("id");
            parentDiv= parentDiv.split('-');
            paId=parentDiv[1];
                field_arr[Id]=new Array();
                field_arr[Id]['paId']=paId;
                field_arr[Id]['top']=topPos;
                field_arr[Id]['left']=left;


            });


            console.log(field_arr);

Output Is: 输出为:

[undefined, [] left 140 paId "1" top 10 [undefined,[]左140 paId“ 1”前10名

What is problem in It Any help Should be appreciated. 它有什么问题任何帮助都应得到帮助。

The problem is in the display method of your arrays. 问题出在数组的显示方法中。 The information is there, but both alert and console.log will not show it to you because it is expected that the only interesting properties of arrays are the ones with numeric indexes. 该信息在那里,但是alertconsole.log都不会显示给您,因为期望数组唯一有趣的属性是带有数字索引的属性。

In JavaScript, unlike PHP, objects are used as maps/associative arrays. 在JavaScript中,与PHP不同,对象被用作地图/关联数组。

First to check that your information is actually there: 首先检查您的信息是否确实存在:

$(".dr").each(function(index){
  var Id=$(this).attr("id");
  console.log(Id, field_arr[Id]['paId'], field_arr[Id]['top'], field_arr[Id]['left']);
});

Now to make make the display methods work you can go about multiple ways, but the best one is to use objects instead: 现在,使显示方法起作用,您可以采用多种方法,但是最好的方法是使用对象:

var field_arr = Object.create(null); // replace with {} if you want to support IE8-

$(".dr").each(function(index){
  var id = $(this).attr("id"); // added var to keep variable local
  var drag = $("#"+dragId);

  field_arr[id] = Object.create(null); // {}

  field_arr[id]['paId'] = drag.parent().attr("id").split('-')[1];
  field_arr[id]['top']  = drag.position().top;
  field_arr[id]['left'] = drag.position().left;
});

console.log(field_arr);

Iterating over properties of objects is quite easy: 遍历对象的属性非常容易:

for (var id in field_arr) {
  console.log(field_arr[id], field_arr[id]['paId'], 'etc');
}

Add a hasOwnProperty check if your object doesn't inherit from null ( var obj = {} needs it, unlike var obj = Object.create(null) ) 添加hasOwnProperty检查是否您的对象不是从null继承的( var obj = {}需要它,这与var obj = Object.create(null)

you're storing values with a key string and its wrong because you declared your field_arr as a numerical array (well there's no such thing as associative array in javascript i think). 您正在使用键字符串存储值,而这是错误的,因为您将field_arr声明为数值数组(我认为JavaScript中没有关联数组之类的东西)。

field_arr[Id] = new Array();
field_arr[Id]['paId']=paId; //this is wrong

You need to create an object to store in values as if they are associated with string keys. 您需要创建一个对象来存储值,就像它们与字符串键相关联一样。 But literally they are object properties 但从字面上看,它们是对象属性

redeclare it like this 像这样重新声明

field_arr[Id] = {}; //you create an object
field_arr[Id]['paId'] = paId; //create an object property named paId and store a value
field_arr[Id].paId = paId; //you can also access property paId like this

EDIT: but to conform to you current code you can access your indexes using strings by accessing it like a property of an object. 编辑:但是为了符合您当前的代码,您可以通过像对象的属性一样访问它来使用字符串访问索引。 (Thanks to Tibos ) (感谢Tibos

var field_arr=[];
...
...
field_arr[Id].paId = paId; 

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

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