I am trying to create a table dynamically nxm with random data, where n is the number of columns and m the number of rows and I want to display these data in a table in html:
For example, if they ask me for the table to be 4 x 3 , I want an array of objects to be randomly created like the following:
obj = [
{ 1: 255, 2: 685, 3: 483 },
{ 1: 982, 2: 159, 3: 753 },
{ 1: 951, 2: 632, 3: 478 },
{ 1: 746, 2: 851, 3: 118 }
]
I am working on it in a class in the following way but I am stuck:
getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min)
}
for (let index = 1; index <= f.value.columnas; index++) {
this.numeroRandom.push(this.getRandom(f.value.rangoMin, f.value.rangoMax));
}
console.log(this.numeroRandom);
for (let index = 1; index <= f.value.columnas; index++) {
this.arrayColumnas.push(index);
}
for (let index = 1; index <= f.value.filas; index++) {
this.arrayFilas.push(index);
}
I tried to use this option but I couldn't Object.assign()
You're doing things the other way around, creating the columns first, but you're using
.push()
, which is a method for arrays. The steps to follow are:[]
{}
Important: According to the JSON specifications
So despite assigning numeric keys, they will always be treated as strings.