Reputation: 25
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.
Upvotes: 0
Views: 208
Reputation: 6770
In order to use push()
function in the key 40 you have to initialize that position as an array as well.
var tdarray = [];
tdarray[40] = [];
tdarray[40].push(22)
// It works now
alert(tdarray[40][0]);
In the code you provided:
var tdarray = [];
...
// Check if it's an array before initialize
if(!tdarray[this.name] instanceof Array) {
tdarray[this.name] = []; // or tdarray[this.name] = new Array();
}
tdarray[this.name].push(tval);
Upvotes: 3
Reputation: 2973
You have to check if its an array already and if its not create it.
var tval = $(this).val();
check=this.name;
if( tdarray[this.name]constructor !== Array){
tdarray[this.name]=[]
}
tdarray[this.name].push(tval);
Upvotes: 0
Reputation: 657
Maybe obvious solution, but if you want to be able to access your values by a key (such as 40), why not use an object?
var tdarray = {};
if ( ! tdarray['40']) tdarray['40'] = [];
tdarray['40'].push(22)
Upvotes: 0