How to make data in an array the keys of a JSON file?

I was processing some data and I have filtered the data to a point where I have stored the data which should be the keys of the JSON file I want to create into an array like this

[
  'A',
  'B',
  'C',
  'D',
  'E'
]

Now I want to set these array values as keys dynamically to create a JSON with other data

I tried as

        for(let i=0;i<arrdata.length;i++){
          let row;
          if(arrdata[i]*1 != typeof(Number)) {
            row = {
              keys[i] : arrdata[i]
            }
          } 

but herre it’s showing a error saying

‘,’ expected

How can I do this ?

Oh this is specific to the data I have. My data includes an array full of strings but some of the data are ‘1’, ‘2’ etc…
So I wanted to split the data at that point so I put it like that for testing

I saw that if we multiply a string with a number we would get NaN unless the string contains the number so I thought this check would be a good point to split the data

I have the what is supposed to be keys stored in an array called keys and the data that should be put against it in an array called arrData

The data in arrdata looks like this

'1',
  '<redacted>',
  '<redacted>,
  '<redacted>',
  '<redacted>',
  'PASS',
  'PASS',
  '2',
  '<redacted>',
  '<redacted>',
  '<redacted>',
  '<redacted>',
  'PASS',
  'PASS',

I wanted to split the data at the number value so I put the condition like that

My issue is not exactly with the condition but how to make the values in keys the key of the JSON I’m trying to construct

Hi This is what the keys should look like

[
  'A',
  'B',
  'C',
  'D',
  'E'
]
    row = {
      keys[i] : arrdata[i]
    }

To make the value of keys[i] the key for the value arrdata[i], you must surround keys[i] with [ and ].

    row = {
      [keys[i]] : arrdata[i]
    }
1 Like

The arrdata values are part of the key:value pair of the JSON I want to construct

the keys being the values in the array keys and the value part coming from values in the array arrdata

This worked. Thank you. Now let me figure out rest of the logic to construct my JSON. Thanks again

I agree that this looks like a hack. Even if it seems to do what you want, this is probably not how you want to do this.

Yeah I know, but how else am I supposed to check whether the data inside is a number when the number is like '1', '2'

Please consider the fact that I reduced the relevant data from a large chunk of data to this so I had no control over it.

If you have a better way please do suggest it because I’m a JS newbie

Can I try instead of this

arrdata[i]*1 != NaN

Instead of relying upon implicit conversion, try using explicit conversion. Googling something like ‘convert string to number, JavaScript’ is where I’d start - I never remember exact function names.

Then you can check if the conversion worked.

1 Like

The isNumber util worked for this

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.