0

I am using csv-parse in node.js (sync api) to parse a csv into a js object. Everything is working great except I'm having a lot of trouble accessing the first key value pair.

If I console log the resulting object I get

//object scorecard
{
  'PlayerName': 'Par',
  CourseName: 'Lismore Park Wanaka',
  LayoutName: 'Mid 2020 (New Hole Numbering)',
  Date: '01-07-21 12:44',
  Total: '55',
  '+/-': '',
  Hole1: '3',
  Hole2: '3',
  Hole3: '3',
  Hole4: '3',
  Hole5: '3',
  Hole6: '3',
  Hole7: '3',
  Hole8: '3',
  Hole9: '4',
  Hole10: '3',
  Hole11: '3',
  Hole12: '3',
  Hole13: '3',
  Hole14: '3',
  Hole15: '3',
  Hole16: '3',
  Hole17: '3',
  Hole18: '3'
}

Trying to access PlayerName I get undefined, I'm assuming because PlayerName is in quotes (this object is made by csv-parse). scorecard.PlayerName and scorecard['PlayerName'] don't work. However, if I use..

let keys = Object.getOwnPropertyNames(scorecard) 
console.log(scorecard[keys[0]])

I do get the desired result of Par.

lucai
  • 1
  • 1
    It obviously would have to be `scorecard["'PlayerName'"]` (or something comparable), because you want the single quotes to be part of the value, and not just string delimiters on the _code_ level. – CBroe Jul 15 '21 at 10:25
  • If you assign the object to a variable called scorecard and you access it like `scorecard['PlayerName']` It must work and I cannot see any reason for it to not to work!, unless the values are not populated yet or the variable is empty all the time! – HSLM Jul 15 '21 at 10:35
  • @CBroe tried the ["'PlayerName'"] didn't work. If I console log keys[0] it spits out a string PlayerName (no quotes) – lucai Jul 15 '21 at 21:36
  • @HSLM yeah..I'm pretty confused. logging scorecard[0]['PlayerName'] and scorecard[0][keys[0]] right next to each other are undefined and 'Par' sorry, left out scorecard is an array of objects, this is object 1, don't think that should make a difference – lucai Jul 15 '21 at 21:38

2 Answers2

1

var a = {
  'PlayerName': 'Par',
  CourseName: 'Lismore Park Wanaka',
  LayoutName: 'Mid 2020 (New Hole Numbering)',
  Date: '01-07-21 12:44',
  Total: '55',
  '+/-': '',
  Hole1: '3',
  Hole2: '3',
  Hole3: '3',
  Hole4: '3',
  Hole5: '3',
  Hole6: '3',
  Hole7: '3',
  Hole8: '3',
  Hole9: '4',
  Hole10: '3',
  Hole11: '3',
  Hole12: '3',
  Hole13: '3',
  Hole14: '3',
  Hole15: '3',
  Hole16: '3',
  Hole17: '3',
  Hole18: '3'
};

console.log(a['PlayerName'])
Ernesto
  • 3,944
  • 1
  • 14
  • 29
0

After the sanity checks provided above (thank you) found a 0 width no break space character at the beginning of the Object Property in question.

lucai
  • 1