1

I have two columns with name and age.The jason returns the following object.I would to return the value under name and age column.

 Students: {
  details: [
   { John: 21 },
   { Brian: 22 },
   { Ryan: 21 },

     <tbody>
        <tr v-for= "(item,index) in Students.details" :key="index" >
          <td ">
            {{item --(should display name)}}
          </td>
          <td">
            {{item --(should display age)}}
          </td >
        </tr>
      </tbody>

2 Answers 2

1

This could work out:

<tbody>
        <tr v-for= "(item, index) in Students.details" :key="index" >
          <td>
            {{Object.keys(item)[0]}}
          </td>
          <td>
            {{item[Object.keys(item)[0]]}}
          </td >
        </tr>
</tbody>
Sign up to request clarification or add additional context in comments.

Comments

0

There are many ways to solve your problem, but I suggest to arrange the data first to a simpler form then render them. For example, use computed to change the form first.

computed: {
  dataArray() {
    return Object.keys(this.Students).map(name => {
        return {
          name: name,
          age: this.Students[name]
        }
    })
  }
}
// you can get an array [{name: "John", age: 21}, {name: "Brian", age: 22}, ...]

Then in the template just show the data:

<tr v-for= "item in dataArray" :key="item.name">
  <td>
  {{ item.name }}
  </td>
  <td>
  {{ item.age }}
  </td >
</tr>

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.