Inigo Inigo - 1 year ago 81
Node.js Question

JS/Nodejs can't access quoted index of object

I use babyparse (PapaParse) in nodejs to process csv to javascript objects.

One object output is as follows:

{ 'ProductName': 'Nike t-shirt',
ProductPrice: '14.99',
ProductPriceOld: '39.99' }

You can see that somehow the first index is quoted ('ProductName').
I can't manage to access this value. I have tried:


This all results in 'undefined'. product.ProductPrice or product['ProductPrice'] gives me the correct value.

Any idea what can be causing this, and how to solve it?

Edit: if I JSON.stringify(product) the single quotes are gone, but if I JSON.parse(json) into the object, the single quotes return..?

Answer Source

The reason that the property name is expressed as a string instead of an identifier in the output is that the name includes characters which aren't allowed in an identifier.

Specifically, the first character is: U+FEFF : ZERO WIDTH NO-BREAK SPACE [ZWNBSP] (alias BYTE ORDER MARK [BOM]).

You need to include that in the property name when you access it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download