Add and Find value in Multidimensional Array in JavaScript

Last Reply 10 months ago By dharmendr

Posted 10 months ago

how to assign value to multidimensional array in javascript and how to find value.

i tried

        MultiArray = new Array(5);
        MultiArray[0, 0] = "Total Task"
        
        MultiArray[0, 1] = TotalTask
        
        MultiArray[1, 0] = "Completed"
        MultiArray[1, 1] = Completed_Task
        MultiArray[2, 0] = "Rejected"
        alert(MultiArray[0, 0]);
        MultiArray[2, 1] = Rejected
        MultiArray[3, 0] = "Pending"
        MultiArray[3, 1] = Pending;
        MultiArray[4, 0] = "Uncomplete"
        MultiArray[4, 1] = uncomplete

when anywhere i try to find value, it always shows last value.

on alert of [0,0] it showed rejected

Posted 10 months ago

Hi mukesh1,

Check this example. Now please take its reference and correct your code.

<script type="text/javascript">
    window.onload = function () {
        var TotalTask = 5;
        var Completed_Task = 2;
        var Rejected = 1;
        var Pending = 1;
        var Uncomplete = 1;

        var multiArray = new Array(5);
        for (var i = 0; i < multiArray.length; i++) {
            multiArray[i] = new Array(2);
        }

        multiArray[0][0] = "Total Task";
        multiArray[0][1] = TotalTask;

        multiArray[1][0] = "Completed";
        multiArray[1][1] = Completed_Task;

        multiArray[2][0] = "Rejected";
        multiArray[2][1] = Rejected;

        multiArray[3][0] = "Pending";
        multiArray[3][1] = Pending;

        multiArray[4][0] = "Uncomplete";
        multiArray[4][1] = Uncomplete;

        alert(multiArray[0][0]); // alert 1st index 1st item i.e. Total Task
        alert(multiArray[0][1]); // alert 1st index 2nd item i.e. 5
        alert(multiArray[0, 0]); // alert 1st index i.e. Total Task,5
    }
</script>

Demo