Error inserting object

1

I want to insert objects in an array but it does not work

$(this).find("input").each(function (index) {
                    console.log('INDEX', index);
                    arreglo.push({index: $(this).val()});
                });

arreglo.push({index: in this part index is not recognized, why?

    
asked by hubman 03.09.2018 в 18:36
source

1 answer

1

What you have been doing is wrong, instead of trying to name the index in the {index: $(this).val()} object, you should only pass it the value of the input.

// inicializo el array
var arreglo = [];

$(this).find("input").each(function (index) {
    // agrego al array el valor del input
    arreglo[index] = $(this).val();
});

console.log(arreglo);

// accedo a un valor del array
console.log(arreglo[1]);
    
answered by 03.09.2018 / 19:31
source