Rodrigo Rosa
Rodrigo Rosa

Reputation: 3

Return value of object in an array

I have the following statement:

const queue = [
  {name: "Click2Call"}
]
return queue.name

This returns null.

How can I fix it to return the object name?

Upvotes: 0

Views: 129

Answers (2)

RoToRa
RoToRa

Reputation: 38390

What are you trying to do? This

queue:[{"name": "Click2Call"}]

is syntactically correct, but doesn't do anything. If you are trying to create a variable called queue you need to declare the variable with const or let (or var) and use the assignment operator =. For example:

const queue = [{"name": "Click2Call"}]

But this is also an array which doesn't have a name property. You need to access the first element of the array. For example:

return queue[0].name

Upvotes: 1

Brandon.G
Brandon.G

Reputation: 79

You're trying to access an array with a key. You can try: return queue[0].name

But that is not robust and can fail if the array is empty or if the 0 indexed value doesn't have that key.

Upvotes: 0

Related Questions