key with "-" RuntimeError: ag is not defined

I pulled a csv with a key value pair of
0: Object {
date: “2020-01-24”
rtk-ag: “0”
pcr: “2”
}

When I tried to map them, it says :

> testNumber2 = RuntimeError: ag is not defined as there is a - in the key …

How do I fix this error ?

testNumber2 = testNumbers.map( d => (
    {
      ...d,
      rtk: d.rtk-ag,
      date: dateParser(d.date)
    } 
   )
 )

2nd Question, I uses parseInt() to convert string to number, but it gives me NaN

totalTest =  testNumbers.map( d => (
    {
      date: dateParser(d.date),
      test:  parseInt(d.pcr)
    } 
   )
 )

You’ll have to use [] syntax to reference that field: d[‘rtk-ag’] should work. And what value is the string you’re passing to parseInt that gives you NaN?

Thanks, error went way but now I got tan undefined

**Object {date: 2020-01-24T00:00, rtk-ag: “0”, pcr: “2”, rtk: undefined}

And what value is the string you’re passing to parseInt that gives you NaN?
I am passing numbers / integers to it…

 0: Object {date: "2020-01-24",  rtk-ag: "0",  pcr: "2"}
  1: Object {date: "2020-01-25",  rtk-ag: "0",  pcr: "5"}
  2: Object {date: "2020-01-26",  rtk-ag: "0",  pcr: "14"}
  3: Object {date: "2020-01-27",  rtk-ag: "0",  pcr: "24"}
  4: Object {date: "2020-01-28",  rtk-ag: "0",  pcr: "53"}
  5: Object {date: "2020-01-29",  rtk-ag: "0",  pcr: "71"}
  6: Object {date: "2020-01-30",  rtk-ag: "0",  pcr: "42"}
  7: Object {date: "2020-01-31",  rtk-ag: "0",  pcr: "47"}
  8: Object {date: "2020-02-01",  rtk-ag: "0",  pcr: "28"}

No. If you remove the parseInt function, you’ll see that you’re d.pcr is undefined. The reason is that 'pcr' is not a key - ' pcr' is. Thus, you could call d[' pcr'] or redefine the keys to get rid of that annoying space.

2 Likes

Thanks, there was a hidden space before the rtk-ag and pcr

Now parseInt() works…

testNumber2 = testNumbers.map( d => (
    {
      pcr: parseInt(d[' pcr']),
      rtk: parseInt(d[' rtk-ag']),
      date: dateParser(d.date)
    } 
   )
 )
2 Likes