Shahmee Shahmee - 1 month ago 12
Javascript Question

set jquery multidimensional array

I'm getting attribute values. I need to set into multidimensional array but it's showing error. where im getting wrong ?

var myArray = [];
amount=10;
x=1
$(id).closest('td').nextAll().find('input').each(function (n) {

myArray[x]['id'] = $(this).attr('data-id');
myArray[x]['year'] = $(this).attr('data-year');
myArray[x]['month'] = $(this).attr('data-month');
myArray[x]['amount'] = amount;

x++;
});
console.log(myArray);

Answer

you are missing this line

myArray[x] = {};

before this line

myArray[x]['id'] = $(this).attr('data-id');

since you need to initialize this object first before setting properties to it.