-1

How i can select the count(user) from the tab object?

var tab =
[
    { id: '1', 'count(user)': 11 },
    { id: '2', 'count(user)': 31 },
    { id: '3', 'count(user)': 3 }
]

This prints 2:

console.log(tab[1].id)

but this gives me an error:

console.log(tab[1].count(user))

ReferenceError: user is not defined

How can I fix this?

Siguza
  • 21,155
  • 6
  • 52
  • 89
elreeda
  • 4,525
  • 2
  • 18
  • 45

1 Answers1

1

Like this:

alert(tab[1]["count(user)"]);

Here is an example: JSFiddle

By the way, while the JSON in your example technically works, it is not formatted in a "best practice" way. Parameter names should have quotes around them:

{ 'id': '1',...

And parameter names should be valid JavaScript variables names, ie., no parenthesis, brackets, etc. This would be a more acceptable name:

'count_user': 11

Then you would not have had an issue using

console.log(tab[1].count_user)
Community
  • 1
  • 1
jwatts1980
  • 7,254
  • 2
  • 28
  • 44