Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add array value to key javascript [duplicate]

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 ?

like image 629
Mihai Vilcu Avatar asked Jun 30 '26 21:06

Mihai Vilcu


1 Answers

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);
like image 133
Tomáš Zato - Reinstate Monica Avatar answered Jul 02 '26 11:07

Tomáš Zato - Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!