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'm trying to create an object which references a number of forms which I can JSON.stringify() to send through to a validation script with a single AJAX request, but I can't seem to properly name the array inside the object, which should be an incrementing count as the number of arrays (forms) increases.

i.e.

var images = new Object();
var i = 0;

// loop through every form
$('form').each(function() {
   var form = $(this);

   images[i].name = form.find('input[name="name"]').val();
   images[i].description = form.find('textarea[name="description"]').val();

   // etc.

   i++;
});

So, when complete after say two to three iterations (that is, it's gone through two-three forms), I have a Javascript object similar to this (written in pseudocode, I'm not exactly too sure how it's actually outputted):

images {
   0 {
      name : 0thImageNameValueHere,
      description : 0thImageDescripValueHere,
      etc : etc
   }

   1 {
      name : 1stImageNameValueHere,
      description : 1stImageDescripValueHere,
      etc : etc
   }
}

But, right now, Firebug is giving me a SyntaxError: missing ; before statement error, centered around this line:

images[i].name = form.find('input[name="name"]').val();

Now, I can change the 'value' of images[i].name to anything I like (images[i].name = 'yes') and I still get the same error. Syntactically I'm not missing any semi-colons, so it can't be that. Is it possible I'm not declaring my object correctly?

share|improve this question
    
You define images as an object, but isn't it an array with objects? –  Justus Romijn Mar 12 '13 at 10:19
    
I'm not exactly sure on the formal language in Javascript, but if it were PHP, it would be the equivalent to a multidimensional associative array, with image attributes forming key => value pairs. –  Antilogical Mar 12 '13 at 10:25
    
You can use either in javascript as long as your index is an int, if you want to use strings as keys, you must use an object. –  Tetaxa Mar 12 '13 at 10:28

2 Answers 2

up vote 1 down vote accepted

I don't know about any missing semi colon, but you need to create an object at images[i] before you assign any properties on it. Ie try this:

images[i] = { 
    name: get('input[name="name"]').val(),
    description: get('textarea[name="description"]').val()
};

You can also use the index parameter supplied by each():

$('form').each(function(i) { ... }
share|improve this answer
    
This seems to be working... can I pass this to JSON.stringify and then to $.ajax and on the other end (a PHP script) use json_decode(); to get an array? –  Antilogical Mar 12 '13 at 10:30
    
yes, that'll work. –  Tetaxa Mar 12 '13 at 10:32

Images is an array ([]). Your syntax does not comply with this situation (you expect an object). Create an object for each item in the array, then you can assign values to the attributes of this object.

Also, you can just make use of the index parameter provided by jQuery, you don't have to create your own iterator.

This does what you want:

var images = []; // create an array

$('form').each(function( index ) {
    var form = $(this);
    // create object with {} object notation
    var image = {
        name: form.find('input[name="name"]').val(),
        description: form.find('textarea[name="description"]').val()
    };
    images[index] = image; // append object in array position index;
}

See for more info on objects the JSON WIKI. See for more info on arrays W3Schools.

share|improve this answer
    
What exactly is the difference between var images = []; var images = {}; and var images = new Object()? Can I still pass this 'array' to JSON.stringify() successfully? –  Antilogical Mar 12 '13 at 10:32
    
an array is a list of anything [1,2,3] but does not support key-value pairs. Objects do support those, see my updated example on how to set these key-value pairs using the object notation –  Justus Romijn Mar 12 '13 at 10:40

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.