0

I'm trying to expand some Javascript code that creates JSON objects based on a input integer. The original integer spanned from 1 to 3 so the Javascript code used a single fixed piece of code for the JSON object creation. Here is the code and it's ouput:

function makeObject(stationNumber) {
  if (stationNumber == 1) {
    var obj = {
      'station_1': [{
        'totalEfficiency': 0,
        'AvgTotalPass': 0,
        'AvgTotalFail': 0,
        'actualBulidTime': 0,
        'idealBulidTime': 0,
        'isWorking': 'true'
      }]
    }
    console.log(obj)
    return obj;
  }
}

Output:

{ station_1:
   [ { totalEfficiency: 0,
       AvgTotalPass: 0,
       AvgTotalFail: 0,
       actualBulidTime: 0,
       idealBulidTime: 0,
       isWorking: 'true' } ] }

The station number is now expanding from a max of 3 to a max of 32 so I need to make the object creation use the input station number to create the object vs. the fixed code that was used. I've tried variations of this code:

var stationString = "string"
stationString = 'station_' + stationNumber.toString();
var obj = { stationString: [ 
         {'totalEfficiency': 0, 'AvgTotalPass': 0, 
          'AvgTotalFail': 0, 'actualBulidTime': 0, 
          'idealBulidTime': 0,'isWorking': 'true'} ] }

Created Object:

{ stationString:
   [ { totalEfficiency: 0,
       AvgTotalPass: 0,
       AvgTotalFail: 0,
       actualBulidTime: 0,
       idealBulidTime: 0,
       isWorking: 'true' } ] }

Clearly the modification is not working correctly and searching for an answer that is similar to this has not been fruitful. I am very unfamiliar with Javascript(only "C" and Python) so any help would be appreciated.

Barmar
  • 741,623
  • 53
  • 500
  • 612
lynnejohn
  • 25
  • 6
  • 1
    Put it in square brackets: `[stationString]: [...]` – Barmar Jun 22 '20 at 20:07
  • Sorry, I don't see the duplicate and I've seen those other posts. Is it that having Javascript create JSON objects is so obscure that there are so many 'similar but not exactly' questions ? – lynnejohn Jun 22 '20 at 20:23
  • It's in the first answer in the duplicate. `iconMap : { [KEYS.PHONE_TYPE] : 'icon-phone', [KEYS.AGENT_TYPE] : 'icon-headphones' };` – Barmar Jun 22 '20 at 20:27
  • If you ask me the data structure layout looks nothing like the first answer of that post(or the second, third or fourth.) Maybe it's because coming from a different background I expect to see an answer with near identical syntax to my issue. Maybe having the right keyword to search for would have helped me understand the JSON structure or Javascript object creation. Is the "station-1" field a "dynamic keyname" in the object? – lynnejohn Jun 22 '20 at 20:50
  • You're not going to find exact duplicates. You should recognize the general pattern: you want to use an expression in the key, you put it in square brackets. A single variable and an expression like `KEYS.PHONE_TYPE` are equivalent in this context. – Barmar Jun 22 '20 at 20:57
  • JavaScript objects have properties, and the properties have names or keys. So this is a dynamic property name. – Barmar Jun 22 '20 at 20:58
  • Thanks for your help. Recognizing the general pattern would mean that I have a better understanding of Javascript. I tried looking through examples and then for similar problems but nothing that I could denote was my issue. I have to agree with the original poster in that I looked but did not feel I had found an answer. I appreciate the feedback. – lynnejohn Jun 22 '20 at 21:24
  • I understand why you might not be able to find the duplicate on your own, since you didn't know what to look for. But once I linked them, I think you should be able to see the similarity -- they're both using a computed value for the key. – Barmar Jun 22 '20 at 21:25

0 Answers0