I am inserting values in two dimensional array according to my role_id i.e
var tdarray = [[]];
tdarray[40].push(22);
where 40 is my role_id and 22 is its value. However when i print this value it shows null
alert(tdarray[40][0]); //this shows no value.
I guess two dimensional array in jquery does not allow to insert values at specific position.Can you suggest me what i can do to overcome this. Entire Code is here
var tdarray = [[]];
var incr = 1;
var check;
$(function () {
$('.toggle_checkbox').change(function () {
if (check === null) {} else {
if (this.name == check) {
incr++;
} else {
incr = 1;
}
}
var tval = $(this).val();
check = this.name;
tdarray[this.name].push(tval);
});
});
Html code
<table border = "0"
cellspacing = "0"
cellpadding = "1" >
<tbody>
<c:forEach items="${accessRightValues}" var="rights" >
<tr style="height: 40px;">
<td style="width: 240px;"><input type="checkbox" name="<c:out value="${rights.roleid}"/>" value="1" class="toggle_checkbox"> Add </td>
<td style="width: 240px;"><input type="checkbox" name="<c:out value="${rights.roleid}"/>" value="2" class="toggle_checkbox">Update</td>
<td style="width: 240px;"><input type="checkbox" name="<c:out value="${rights.roleid}"/>" value="3" class="toggle_checkbox">View </td>
<td style="width: 240px;"><input type="checkbox" name="<c:out value="${rights.roleid}"/>" value="4" class="toggle_checkbox">Delete </td>
<td style="width: 240px;"><input type="checkbox" name="<c:out value="${rights.roleid}"/>" value="5" class="toggle_checkbox">Assign </td>
</tr>
</c:forEach>
</tbody> < /table>
My problem is that id 40 can hold more than one value .So i want to know how i can do it using multidimensional array in jquery.Also there can be more than one role_id's such as 50,57 which will again hold more than one value.Please help me regarding the same. I want to pass this two dimensional array in my spring controller. tdarray[40][0] =1; tdarray[40][1] =3; tdarray[40][2] =5; tdarray[48][0] =2; tdarray[48][1] =3;
where 40,48 is role_id of a user and 1,3,5 are access_rights which i want to store in database.
tdarray[40] = [];and then push a value inside it :tdarray[40].push(22);. That said, your array should be declared as:var tdarray = [];. And, finally, I would rather recommend you an object for such a scope:var tdobject = {};tdobject[40] = [22];jsfiddle.net/ok96wwyfvar tdarray = [[]];is not a multi-dimensional array. It's an array with one element which is another (empty) array; Your next line withpushthrows exception becausetdarray[40]is undefined. You have to dotdarray[40] = [22];. Later on you can push new valuestdarray[40].push(23);and this will become a md arraytdarray[this.name] = tdarray[this.name] instanceof Array ? tdarray[this.name] : [];before:tdarray[this.name].push(tval);. This will check iftdarray[this.name]is an array. If so, it just let it be and push the next value inside it, else it will initialize it as an array.