0

I am creating javascript two dimensional array

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

What is problem in It Any help Should be appreciated.

8
  • javascript's arrays are only numeric, for associative "arrays" you should use objects as far as I know Commented Jan 22, 2014 at 8:09
  • @aleation You are wrong. JS arrays are objects and can be used as regular objects. It is usually not semantically correct to do so, but it can be done. Commented Jan 22, 2014 at 8:12
  • 1
    Wrong. All objects have only string indexes, including arrays. Arrays just do something special with string indexes that look like numbers. var a = []; a.dodge = 'such code'; console.log(a.dodge); Commented Jan 22, 2014 at 8:16
  • 1
    Youre right! then the only problem OP is having is that he's using ['index'] instead of .index I guess, hope he reads this xD Commented Jan 22, 2014 at 8:18
  • 1
    @aleation a['index'] is entirely equivalent to a.index. In any case that a.index works, a['index'] will work as well. As for Mahan, you were probably doing the right thing creating objects. There are few cases where you actually need properties on a specific array. Commented Jan 22, 2014 at 8:24

2 Answers 2

1

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.

In JavaScript, unlike PHP, objects are used as maps/associative arrays.

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))

Sign up to request clarification or add additional context in comments.

Comments

0

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[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)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.