1

I have to book (musical) notes in an assoc. array. The booking variable is:

var table = {
    "E": [],
    "F": [],
    "G": [],
    "A": [],
    "H": [],
    "c": [],
    "d": [],
    "e": [],
    "f": [],
    "g": [],
    "a": [],
    "h": [],
    "c'": [],
    "d'": [],
    "e'": [],
    "f'": [],
    "g'": [],
    "a'": [],
    "h'": [],
    "c\'\'": [],
    "d\'\'": []
};

When I parse my notes the values (note positions) are inserted without any problem. But when I would like to get the value, it's not possible to use the key c', whereas e.g. e, a, d work. Why can c' accept values but can't be found as a key? Where must I look for an explanation?

Marc B
  • 356,200
  • 43
  • 426
  • 500

2 Answers2

3

How are you accessing that key?

table["c'"] = 'foo'; // work as expected
table.c' = 'foo'; // syntax error due to unterminated string
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

If you want to use an object property that is not a valid JavaScript identifier, make sure that you use bracket syntax […], like this:

console.log(table["c'"]); // works

Further Reading

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331