Home > Net >  create an object of objects using FOR Loop
create an object of objects using FOR Loop

Time:07-10

I am trying to create a JSON object in javascript (node-red) in this format:

gaugeData.channel.degrees:

gaugeData = {
    ch1: {10: 'value',
            20: 'value',
            ...
            360: 'value'}
    ch2: {10: 'value',
            20: 'value',
            ...
            360: 'value'}
    ...
    ch8: {10: 'value',
            20: 'value',
            ...
            360: 'value'}
    

So that is a JSON object: "gaugeData" will have 8 properies called "channel" and each channel has 36 properties called, 10, 20 ,30 ,40 ... 360.

So far I have tried:

for(var i = 1; i <= 8; i  ){
    for(var c = 10; c <= 360; c  = 10){
        var channels = {
            [i]: {
                [c]: randInt(),
            }
        }
        flow.set("gaugeData", channels); 
    }
}

This gives me:

{"8":
    {"360":250}
}

So i can see my loop is working, though no matter what i cannot get each elemnet to append to the "gaugeData" object. It is only storing the last iteration of the loop. I have tried gaugeData.append, and gaugeData.push, but having similar no good results there.

What method should i use to append each iteration to the gaugeData object?

CodePudding user response:

You basically have it, just store the values in a parent object before calling the next iteration of each for loop. Example:

const createChannels = () =>{
  const topObj = {}
  for(let i = 1; i <= 8; i  ){
    const botObj = {}
    for(let c = 10; c <= 360; c  = 10){
      botObj[c] = Math.floor(Math.random() * 10)
    }
    topObj[i] = botObj
  }
  return topObj
}
const myChannels = createChannels()
console.log(myChannels)

Returns:

{
  '1': {
    '10': 8,
    '20': 4,
    '30': 3,
    '40': 4,
    '50': 8,
    '60': 0,
    '70': 4,
    '80': 4,
    '90': 7,
    '100': 0,
    '110': 2,
    '120': 3,
    '130': 1,
    '140': 1,
    '150': 5,
    '160': 1,
    '170': 0,
    '180': 6,
    '190': 7,
    '200': 7,
    '210': 5,
    '220': 2,
    '230': 2,
    '240': 9,
    '250': 7,
    '260': 0,
    '270': 1,
    '280': 5,
    '290': 1,
    '300': 4,
    '310': 4,
    '320': 9,
    '330': 2,
    '340': 2,
    '350': 6,
    '360': 5
  },
  '2': {
    '10': 6,
    '20': 5,
    '30': 5,
    '40': 2,
    '50': 9,
    '60': 2,
    '70': 9,
    '80': 7,
    '90': 0,
    '100': 9,
    '110': 1,
    '120': 8,
    '130': 0,
    '140': 9,
    '150': 3,
    '160': 7,
    '170': 6,
    '180': 5,
    '190': 5,
    '200': 5,
    '210': 7,
    '220': 9,
    '230': 2,
    '240': 7,
    '250': 0,
    '260': 8,
    '270': 1,
    '280': 2,
    '290': 8,
    '300': 7,
    '310': 1,
    '320': 0,
    '330': 6,
    '340': 0,
    '350': 6,
    '360': 8
  },
  '3': {
    '10': 4,
    '20': 2,
    '30': 5,
    '40': 8,
    '50': 1,
    '60': 2,
    '70': 0,
    '80': 3,
    '90': 9,
    '100': 5,
    '110': 3,
    '120': 8,
    '130': 6,
    '140': 7,
    '150': 7,
    '160': 0,
    '170': 8,
    '180': 7,
    '190': 6,
    '200': 9,
    '210': 5,
    '220': 2,
    '230': 0,
    '240': 1,
    '250': 3,
    '260': 5,
    '270': 5,
    '280': 3,
    '290': 0,
    '300': 1,
    '310': 1,
    '320': 8,
    '330': 0,
    '340': 2,
    '350': 6,
    '360': 4
  },
  '4': {
    '10': 7,
    '20': 6,
    '30': 2,
    '40': 5,
    '50': 1,
    '60': 8,
    '70': 1,
    '80': 1,
    '90': 2,
    '100': 2,
    '110': 1,
    '120': 5,
    '130': 5,
    '140': 4,
    '150': 1,
    '160': 6,
    '170': 2,
    '180': 7,
    '190': 3,
    '200': 0,
    '210': 8,
    '220': 7,
    '230': 2,
    '240': 7,
    '250': 1,
    '260': 5,
    '270': 3,
    '280': 1,
    '290': 1,
    '300': 9,
    '310': 8,
    '320': 7,
    '330': 1,
    '340': 9,
    '350': 7,
    '360': 1
  },
  '5': {
    '10': 7,
    '20': 3,
    '30': 4,
    '40': 4,
    '50': 2,
    '60': 9,
    '70': 1,
    '80': 3,
    '90': 9,
    '100': 9,
    '110': 6,
    '120': 5,
    '130': 9,
    '140': 6,
    '150': 0,
    '160': 1,
    '170': 0,
    '180': 6,
    '190': 8,
    '200': 4,
    '210': 5,
    '220': 5,
    '230': 4,
    '240': 1,
    '250': 4,
    '260': 2,
    '270': 7,
    '280': 0,
    '290': 1,
    '300': 5,
    '310': 0,
    '320': 2,
    '330': 2,
    '340': 3,
    '350': 3,
    '360': 6
  },
  '6': {
    '10': 9,
    '20': 4,
    '30': 9,
    '40': 9,
    '50': 0,
    '60': 7,
    '70': 1,
    '80': 1,
    '90': 4,
    '100': 0,
    '110': 3,
    '120': 1,
    '130': 1,
    '140': 3,
    '150': 9,
    '160': 4,
    '170': 4,
    '180': 8,
    '190': 9,
    '200': 2,
    '210': 6,
    '220': 5,
    '230': 6,
    '240': 0,
    '250': 7,
    '260': 3,
    '270': 9,
    '280': 3,
    '290': 7,
    '300': 8,
    '310': 5,
    '320': 3,
    '330': 4,
    '340': 1,
    '350': 3,
    '360': 7
  },
  '7': {
    '10': 0,
    '20': 7,
    '30': 9,
    '40': 2,
    '50': 6,
    '60': 5,
    '70': 6,
    '80': 6,
    '90': 0,
    '100': 8,
    '110': 3,
    '120': 7,
    '130': 0,
    '140': 9,
    '150': 8,
    '160': 0,
    '170': 9,
    '180': 1,
    '190': 9,
    '200': 3,
    '210': 6,
    '220': 1,
    '230': 3,
    '240': 1,
    '250': 6,
    '260': 6,
    '270': 3,
    '280': 7,
    '290': 4,
    '300': 2,
    '310': 4,
    '320': 5,
    '330': 2,
    '340': 0,
    '350': 1,
    '360': 3
  },
  '8': {
    '10': 4,
    '20': 1,
    '30': 5,
    '40': 3,
    '50': 3,
    '60': 9,
    '70': 4,
    '80': 3,
    '90': 7,
    '100': 2,
    '110': 5,
    '120': 8,
    '130': 4,
    '140': 7,
    '150': 8,
    '160': 3,
    '170': 1,
    '180': 2,
    '190': 5,
    '200': 6,
    '210': 2,
    '220': 2,
    '230': 5,
    '240': 5,
    '250': 1,
    '260': 1,
    '270': 7,
    '280': 8,
    '290': 7,
    '300': 8,
    '310': 3,
    '320': 9,
    '330': 7,
    '340': 2,
    '350': 9,
    '360': 5
  }
}

CodePudding user response:

The channels varaible you have written is initializing everytime the inner loop runs and you are assigning that to gaugeData. So, you will only get the last initialized value.

I have modified the implementation a little bit. Attaching the code for your reference.

for(var i = 1; i <= 8; i  ){
    let channels = [];
    for(var c = 10; c <= 360; c  = 10){
        var channel = {
            [i]: {
                [c]: randInt(),
            }
        }
        channels.push(channel);
    }
    flow.set("gaugeData", channels); 
}

  • Related