Emin Çiftçi Emin Çiftçi - 16 days ago 5
Javascript Question

Intialize an array with random indices in Javascript

I try to fill an array randomly. I have an rows array:

rows = ['1','0','2','1','3'];


and columns array:

columns = ['0','0','1','2','3'];


and a button array:

butArray = [];


I initialized button array. What I try to

printArr[rows[i]][columns[i]] = butArray[i];


How exactly should I use loop here ?

Answer

I think you are looking for this.

https://plnkr.co/edit/1J2VWX0yCIWITQp6Yax8?p=preview

// Code goes here

function myclick(){
  console.log("cds")
  rows = ['1','0','2','1','3'];
  columns = ['0','0','1','2','3'];
  butArray = ['qwe','qwe','qewe','qew','qwe'];//some values
  var printArr = [];
  for(var i=0 ; i<columns.length ; i++){
    if(printArr[rows[i]] == undefined)
    printArr[rows[i]] = []
    printArr[rows[i]][columns[i]] = butArray[i];

  }
  console.log(printArr);
}
Comments