1

I've got a v-for loop in Vue JS 2.x that loops over an Array of objects, each object in the array has a "key", basically like a name, but it's dynamic, and isn't going to be consistent. I'm trying to access the data associated with that key so that I can display information from my v-for loop, here's what my data looks like in my array...

[{
  "6457": {
    "agent": {
      "id": 4003,
      "memFree": 0
    }
  }
}, {
  "7809": {
    "agent": {
      "id": 7809,
      "memFree": 20
    }
  }
}]

I was hoping I could just access the key by using [0], but that doesn't seem to return anything in this case.

<div v-for="(server, index) in servers" :key="index">
  <!-- gives me a single object by key -->
  {{ server }}

  <!-- doesn't work -->
  {{ server.memFree }}

  <!-- doesn't work -->
  {{ server[0].memFree }}
</div>

What am I missing here?

2 Answers 2

2
<div v-for="(server, index) in servers" :key="index">
  {{ Object.values(server)[0].agent.memFree }}
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

Modify your template to be

<div v-for="(server, index) in servers" :key="index">
  <div v-for="(agentData, property) in server" :key="property">
    {{ agentData.agent.memFree }}
  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.