0

I'm creating a project in Vue.js

I need to loop through a JSON object but can't find out how to access this. what I got:

 "functionality": {
        "uitgifte": {
            "image": "",
            "value": "Handmatige capsule inname en uitgifte",
            "label": "",
            "splash": true,
            "compare": false
        },
        "schakelaar": {
            "image": "",
            "value": "Automatische en programmeerbare aan/uit schakelaar",
            "label": "",
            "splash": true,
            "compare": false
        },
        "kopjesrooster": {
            "image": "",
            "value": "Draaibaar kopjesrooster voor latte macchiato-glazen",
            "label": "",
            "splash": true,
            "compare": false
        },
        "ontkalking": {
            "image": "",
            "value": "Automatische melding voor ontkalking",
            "label": "",
            "splash": true,
            "compare": false
        },
        "kopgroottes": {
            "image": "",
            "value": "Programmeerbare kopgroottes",
            "label": "",
            "splash": true,
            "compare": false
        }
    },

and the html:

<div class="tab">
      <table>
        <tbody>
          <tr>
            <td>{{ main.pageCopy.functionele_specificaties }}</td>
          </tr>
          <tr>
            <td v-for="functionality in machine.functionality.uitgifte.value" :key="functionality">{{ machine.functionality.uitgifte.value }}</td>
            <td>
              <img src="" alt="">
            </td>
          </tr>
        </tbody>
      </table>
    </div>

The part that says machine.functionality.uitgifte.value I need the "uitgifte" part to be dynamic so it loops through all the elements like it in the JSON. So "uitgifte, schakelaar, kopjesrooster" etc.

Would be awesome if anyone knows the trick. Thanks.

1

1 Answer 1

1

You simply start your for loop at a higher object level:

<div class="tab">
      <table>
        <tbody>
          <tr>
            <td>{{ main.pageCopy.functionele_specificaties }}</td>
          </tr>
          <tr>
            <td v-for="(functionality, index) in machine.functionality" :key="index">
            {{ functionality.value }}</td>
            <td>
              <img src="" alt="">
            </td>
          </tr>
        </tbody>
      </table>
    </div>

BTW: If you have control of the datasource, make sure to provide an ID for each object and use that ID as the key of the v-for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much! That solved it. Will keep that in mind and pass it along to the ones controlling the data. Cheers!

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.