1

What i'm looking for is the Javascript equivalent of this code:

$map[$card][$bit] = $val;

(as you know this also instantiates the array immediately)

I tried and researched a bit but it seems there are many other ways to do this, i would like to know what is the best way for my specific code. I'm currently retrieving $card and $bit out of a database, and creating some sort of mapping, giving a value to each of the positions in the map.

Javascript code:

var jPunten = JSON.parse(data);             
var puntmap = [][];                         
for (var i=0;i<jPunten.length;i++)
{
   puntmap[jPunten[i].CARDNR][jPunten[i].BITNR] = jPunten[i].STATDEV;
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
Faarbhurtz
  • 550
  • 1
  • 8
  • 27
  • There's no such things as associative arrays in JS, so `puntmap` should actually be object containing another objects. – raina77ow Oct 31 '13 at 13:16

1 Answers1

5

Note:
The first section of this answer assumes you know the difference between arrays and objects in JS, and that the values you're using as keys are numbers, not strings.
First things first. Make sure that, whatever data holds, it's a json representation of an array, and not an object literal. Try console.log(data);, and check your console. If you see something like:

"{"1":"123"}"

Then a for loop just won't cut it, because jPunten is an object, not an array. An array looks like:

"[123,4342]"

Looping an object is done using a for...in loop:

for(var property in jPunten)
{
    if (jPunten.hasOwnProperty(property))
    {
        console.log(jPuntent[property].CARDNR);//this will work
    }
}

But visit json.org for details on the JSON format, and check MDN on, for example hasOwnProperty. MDN also has a nice list, with good documentation on each type of loop you can use in JS.
Anyway, back to the issue at hand:

Numeric keys only:

If you want to initialize a variable to a 2D array om JS, you should declare the second (inner) array inside of the main array.
In short:

var puntmap = [][];//WRONG
//should be:
var puntmap = [[]];

It makes sense, if you think about it: if [] is an array, then [][] are two separate arrays. If one array should be part of the other, then just simply write it inside of that array:

[//an array
    []//first element in the array => an array
]

But given that you seem to be populating a sparse array (check my answer to this question, where I explain the concept of a sparse array), you might want to consider object literals, or just declaring puntmap as any old array.
Then, populate your array like so:

var puntmap = [];
for (var i=0;i<jPunten.length;++i)
{
    //create an array under key jPunten[i].CARDNR, if it doesn't exist already
    puntmap[jPunten[i].CARDNR] = puntmap[jPunten[i].CARDNR] || [];
    //then, assign your value
    puntmap[jPunten[i].CARDNR][jPunten[i].BITNR] = jPunten[i].STATDEV;
}

String constants as keys (properties)

If this assumption (of the jPunten[i].CARDNR etc) are numeric, is false, then do consider using an object straight off. The array will be coerced to an object anyway, only your code won't have anyone believe the length property, and other Array methods are available...

var puntmap = {};//create object literal
for (var i=0;i<jPunten.length;++i)
{
    //create an array under key jPunten[i].CARDNR, if it doesn't exist already
    if (!puntmap.hasOwnProperty(jPunten[i].CARDNR))
    {//if object does not have property
        puntmap[jPunten[i].CARDNR] = {};//create child object literal
        //or, if jPunten[i].BITNR is always numeric:
        puntmap[jPunten[i].CARDNR] = [];//you can use an array here!
    }
    //then, assign your value as you would
    puntmap[jPunten[i].CARDNR][jPunten[i].BITNR] = jPunten[i].STATDEV;
}

Now your code, because you're using object literals, won't confuse anyone, and nobody will attempt to do something like:

puntmap.slice(12,3);

Which, on an object (assoc arrray in PHP-speak) will throw an error.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • The keys are indeed numbers, and WOW, what a magnificent piece of writing, you can tip your hat kind sir ! – Faarbhurtz Oct 31 '13 at 13:42
  • But, i do not understand why you think i am working with a sparse array? I do want all of my elements filled in. The numbers in cardnr and bitnr are ordered and without gaps (only bitnr sometimes has a gap but the range is only from 1-16 so that's ok) – Faarbhurtz Oct 31 '13 at 13:47
  • @PHPnooblet: What I mean is that your `puntmap` array will probably not have a _normal_ index sequence (0,1,2,3...), but, for example `123,4355,56990,...` So the array is [sparse](http://stackoverflow.com/questions/1510778/are-javascript-arrays-sparse). PHP `json_encode` (as you seem to be using php to get the data) returns such arrays as object literals, hence my last edit focusses on the value of `data`. I've also added a link to one of my answers to a question on sparse arrays in JS – Elias Van Ootegem Oct 31 '13 at 13:49