1

I am trying to create a multidimensional array from a few lists I have in my VF controller. The Lists are correct, because when I output them just as individual tables, I get all the correct values. I am trying to load them into an array that looks likes this:

DataSet= [ [Name1, URL1, Login1, Created1],
           [Name2, URL2, Login2, Created2],
           [Name3, URL3, Login3, Created3],
           [Name4, URL4, Login4, Created4],
           .....
          ];

Here is how I am trying to construct the array. I am pushing all the List values into each of their own individual arrays, then looping them into the array in the order I wanted: Name(n), URL(u), Login(e), Created(c).

   <script>
 n = new Array();
     <apex:repeat value="{!names}" var="name">
       n.push('{!name}');
     </apex:repeat>


 u = new Array();
     <apex:repeat value="{!urls}" var="url">
       u.push('{!url}');
     </apex:repeat>

 e = new Array();
     <apex:repeat value="{!logins}" var="logs">
       e.push('{!logs}');
     </apex:repeat>


 c = new Array();
     <apex:repeat value="{!created}" var="create">
       c.push('{!create}');
     </apex:repeat>

var DataSet= new array(5000);

var i=0;
var j=0;

for(i;i<n.length;i++){ 
    for(j;j<3;j++){
        if(j=0) DataSet[i][j] = n[i];
        if(j=1) DataSet[i][j] = u[i];
        if(j=2) DataSet[i][j] = e[i];
        if(j=3) DataSet[i][j] = c[i];

   }
}
alert('test');
</script>

I tested all the arrays, they are display properly, I run into an issue on the looping. Let me know if there's something I did wrong.

Thanks in advanced.

  • What issue do you have with the looping? Why do you have four separate lists? I think it would be easier to have a wrapper in your controller and then repeat over that. Or, use Visualforce JavaScript remoting to retrieve the wrapper into a JS array. – Daniel Hoechst Jan 17 '14 at 16:35
1

First of all, I can't see why would you require nested loops at all. Your logic could have simply been written without the inner loop, being that you have determined number of separate lists:

for(i;i<n.length;i++){ 
    DataSet[i][0] = n[i];
    DataSet[i][1] = u[i];
    DataSet[i][2] = e[i];
    DataSet[i][3] = c[i];
}


However, this wouldn't have worked for you either, since in each iteration you would try to assign a value to the first element of the DataSet[i] object. Let's consider first iteration (i=0) - DataSet[0] is still undefined, as no value has been assigned to the element at that index yet. Accessing any property of that element (e.g. DataSet[0][0] = n[0];) would result in error:

TypeError: DataSet[i] is undefined
DataSet[i][0] = n[i];


What you could do is create temp array in each iteration, and assign it as a value to the current DataSet item, like so:

for(i;i<n.length;i++){ 
   var elm = [n[i], u[i], e[i], c[i]];
   DataSet[i] = elm;
}

This way you would get the multidimensional array you were looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.