Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I was trying to define an array (including other arrays as values) in a single javascript statement, that I can loop through to validate a form on submission.

The function I wrote to (try to) create inline arrays follows:

function arr(){
    var inc;
    var tempa = new Array(Math.round(arguments.length/2));
    for(inc=0; inc<arguments.length; inc=inc+2) {
        tempa[arguments[inc]]=arguments[inc+1];
    }
    return tempa;
}

This is called three times here to assign an array:

window.validArr = arr(
    'f-county',arr('maxlen',10, 'minlen',1),
    'f-postcode',arr('maxlen',8, 'minlen',6)
);

However in the javascript debugger the variable is empty, and the arr() function is not returning anything. Does anyone know why my expectations on what this code should do are incorrect?

(I have worked out how to create the array without this function, but I'm curious why this code doesn't work (I thought I understood javascript better than this).)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Well from what your code does, you're not really making arrays. In JavaScript, the thing that makes arrays special is the management of the numerically indexed properties. Otherwise they're just objects, so they can have other properties too, but if you're not using arrays as arrays you might as well just use objects:

function arr(){
    var inc;
    var tempa = {};
    for(inc=0; inc<arguments.length; inc=inc+2) {
        tempa[arguments[inc]]=arguments[inc+1];
    }
    return tempa;
}

What you're seeing from the debugger is the result of it attempting to show you your array as a real array should be shown: that is, its numerically indexed properties. If you call your "arr()" function as is and then look at (from your example) the "f-county" property of the result, you'll see something there.

Also, if you do find yourself wanting a real array, there's absolutely no point in initializing them to a particular size. Just create a new array with []:

    var tempa = [];
share|improve this answer
    
I'm used to PHP arrays, which I often use flexibly to associate key-value pairs, so was trying to emulate this in js. Thanks! –  Oliver B Apr 11 '12 at 14:28
    
Yes, that's a common source of confusion, and it's not made any better by the fact that JavaScript is perfectly OK with ordinary string-named properties on array objects :-) –  Pointy Apr 11 '12 at 14:38

Your code works. Just inspect your variable, and you will see that the array has the custom keys on it. If not expanded, your debugger shows you just the (numerical) indixed values in short syntax - none for you.

But, you may need to understand the difference between Arrays and Objects. An Object is just key-value-pairs (you could call it a "map"), and its prototype. An Array is a special type of object. It has special prototype methods, a length functionality and a different approach: to store index-value-pairs (even though indexes are still keys). So, you shouldn't use an Array as an associative array.

Therefore, their literal syntax differs:

var array = ["indexed with key 0", "indexed with key 1", ...];
var object = {"custom":"keyed as 'custom'", "another":"string", ...};
// but you still can add keys to array objects:
array.custom = "keyed as 'custom'";
share|improve this answer
    
+1 Thanks for this. The article makes an awful lot of sense. –  Oliver B Apr 11 '12 at 16:14
    
...but you forgot to click on the up-arrow? –  Bergi Apr 11 '12 at 17:30
    
Need a rep score of 15 to upvote answers. I've made a mental note to come back when I can! –  Oliver B Apr 12 '12 at 9:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.