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 ?

Explain what you think this line is doing.

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

That is fine, but what do you think this line is checking? Assume that arrdata is ['A', 'B', 'C'] and i is 1.

Add console.log(typeof(Number)) to the line before the if statement.

Also, what does keys look like?

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

Please post what keys looks like, since you have explained that arrdata looks like in the last post. It is always better to post as much code as possible, so we can test things for ourselves. Currently, your for loop is missing a }. Also, what do you do with row later in the code?

Hi This is what the keys should look like

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

OK, I must be missing something, but I do not understand the connection that exists or you are trying to make between keys and arrdata.

    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.

arrdata[i]*1 != typeof(Number)

FYI - typeof(Number) will always evaluate to "function" which means it will always evaluate to true.

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

typeof(arrdata[i] * 1) !== "number" is what I think you are wanting.

The above checks if the type of whatever arrdata[i] * 1 evaluates to is not a number.

1 Like

This will also always be equal to true.

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