0

I have some JSON which looks like this:

    {
    "data": [
        {
            "name": "someName",
            "link": "http://somelink.com",
            ],
            "relevantArray": [
                {
                    "keyIWant": 42
                }
            ],
            ...
     }

and I get the data like this:

<li v-for="(dataVue, index) in dataList.feat" :key="index">
          {{ dataVue.someName }}
          {{ dataVue.link}}
          {{ dataVue.??? }}
</li>

what do I write to return keyIWant - the name of the key, not the value (42)??

EDIT: I can output the JSON by writing

{{dataVue.relevantArray}}

But this returns the JSON like this:

[ { "relevantArray": keyIwant } ]

and I just can't get the relevantArray to return by itself. CodeSandbox:

https://codesandbox.io/s/festive-wu-41kyz?file=/src/App.vue

13
  • Object.keys(dataVue.relevantArray[0])[0]? Commented May 8, 2020 at 14:46
  • 1
    Have you considered searching for the answer before posting? Have you checked what your index variable holds? Commented May 8, 2020 at 14:46
  • Uh, how do I input that in Vue.js? {{ Object.keys(featData.prerequisite[0])[0] }} is invalid assuming you mean keys = key, but this breaks Vue.js Commented May 8, 2020 at 14:50
  • maybe this? {{dataVue.releventArray[0].keyIwant}} Commented May 8, 2020 at 14:59
  • That returns the value - ie. 42, not the key/name of it. Commented May 8, 2020 at 15:02

2 Answers 2

5

Try this https://codesandbox.io/s/mystifying-brook-42m3x

Basically you can iterate any object as key-value using v-for

<li v-for="(vueData, index) in vueData.data" :key="index">
  {{ vueData.someName }}
  {{ vueData.link }}
   <span
     v-for="(r,ri) in vueData.relevantArray[0]"
     :key="ri"
     >{{ ri }} ==> {{ r }}
   </span>
</li>

will give you something like this

someName http://somelink.com keyIWant ==> 42
Sign up to request clarification or add additional context in comments.

1 Comment

That's almost perfect(!!!!), but in case I got multiple in relevantArray, do I need to wrap it around an additonal v-for?
0

Arief' answer didn't solve the problem for some reason, but I played around with it and found the following solution:

<span
     v-for="(r,ri) in vueData.relevantArray[0]" :key="ri">
      {{ ri }} ==> {{ r }}
         <span v-for="(arrayData, index) in r" :key="index">
            {{index}}
         </span>
   </span>

This makes the output I wanted, doing the above solutions outputted the array in my case. I'll mark Arief' solution as the answer, as it lead me to my solution.

If anyone want to tell me why I had to do my version, I'd be very grateful.

Thanks everyone!!

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.