Skip to content Skip to sidebar Skip to footer

Javascript Multiple Dimensional Array

i have a array [ RowDataPacket { total: 1, deviceName: 'desktop', monthName: 'October' }, RowDataPacket { total: 1045, deviceName: 'desktop', monthName: 'November' }, RowDataPacke

Solution 1:

As mentioned in the comments, the data structure that you want, is not possible in javascript, however, this is the closest you can get to it.

let internalObj = {
  "key1": "value1",
  "key2": "value2"
}

let finalArr = [];

for (let i = 0; i < 5; i++) {
  finalArr.push(internalObj);
}

console.log(finalArr)

The results seems a little messy on stackoverflow, I suggest you try it out on your code.

Solution 2:

It seems like that you want to use OOP in Javascript. You need first define a class and then push instances into an array as follows.

classRowDataPacket {
  constructor(data) {
    this.total = data.totalthis.deviceName = data.deviceNamethis.monthName = data.monthName
  }
}

const arr = [];
arr.push(newRowDataPacket({
  total: 0,
  deviceName: 'mobile',
  monthName: 'October'
}));
arr.push(newRowDataPacket({
  total: 0,
  deviceName: 'tablet',
  monthName: 'October'
}));
arr.push(newRowDataPacket({
  total: 1,
  deviceName: 'desktop',
  monthName: 'October'
}));
// push as many as you want.console.log(arr);

Post a Comment for "Javascript Multiple Dimensional Array"