0

Hi I have an object that I have filled from data in a CSV file.

One of my ids is called "Disk_Usage(MB)"

I try using my object.Disk_Usage(MB) but receive an error because javascript thinks disk usage is a function and that MB is a variable.

I receive a reference error on the console saying MB is not defined.

cching
  • 789
  • 1
  • 8
  • 17

3 Answers3

5

Use array/bracket notation:

var propertyValue = yourObject['Disk_Usage(MB)'];

If you're wondering why you need to do this and/or "but my object isn't an array!," I would suggest having a read through the "Working with objects" article over at MDN.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
3

Do this:

object['Disk_Usage(MB)']

Read this

alexpods
  • 47,475
  • 10
  • 100
  • 94
0

Although object member access can be performed like this:

var o = { 'abc': 123 };
console.log(o.abc);

it is often more convenient to use array-bracket notation:

console.log(o['abc']);

This is one of those times when you must do so, since your key has characters in it that make it an illegal identifier. You must provide it as a string.

Personally, as a matter of consistency, I try to stick to the array-bracket notation all the time.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055