0

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.

7
  • Can you add more details about your 2 dimensional array. tdarray[40].push(22); in this code nothing is related to jquery Commented Jul 8, 2015 at 9:39
  • Technically this is a pure javascript feature (not truly a jQuery feature), and you are doing it in the wrong way: In a nutshell, because 40 is a numeric key, you have to first declare that index as an array: 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/ok96wwyf Commented Jul 8, 2015 at 9:43
  • 1
    Perhaps give us an example of what you want to do with the data and we can suggest a good JS solution. Commented Jul 8, 2015 at 9:43
  • var tdarray = [[]]; is not a multi-dimensional array. It's an array with one element which is another (empty) array; Your next line with push throws exception because tdarray[40] is undefined. You have to do tdarray[40] = [22];. Later on you can push new values tdarray[40].push(23); and this will become a md array Commented Jul 8, 2015 at 9:44
  • After the edit, you can either use an object or just add: tdarray[this.name] = tdarray[this.name] instanceof Array ? tdarray[this.name] : []; before: tdarray[this.name].push(tval);. This will check if tdarray[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. Commented Jul 8, 2015 at 9:48

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

You need to check if it already have a value or have been initialized first before assigning a new array.
@nanndoj-Its working but my problem is that 40 can hold more than one value in that case how can i assign it i.e tdarray[40][0]=5, tdarray[40][1]=4,
Updated my answer to check if the array is initialized before. so it can accept more values
0

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)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.