3

i'm getting the data from a form when it is submitted like this

        values = {};

        $("#myForm").submit(function(){
            $.each($('#myForm').serializeArray(), function(i, field) {
                if(field.name != 'r'){
                    values[field.name] = field.value;

                }
            }); 

            return false;
        });

The problem is that i want to do that multiple times and store all the data in the var values using field.name as a keys and the values as an array to compare it in php i would do values[field.name][] = field.value; is there any similar syntax in js ?

Mihai Vilcu
  • 1,947
  • 18
  • 24

1 Answers1

4

Yeah, you can add multiple values using the Array.push method. But first, you must define values[field.name] as array, like this:

values[field.name] = [];
values[field.name].push(somevalue);
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778