Proper way to show array inside an array with v-for












0















I am trying to display the values of the text array/object but I am getting an output trying to show me paragraphs for every name inside the array/object.



Link to current result: current result



I am fairly new to vue.js so any tips are welcome!





<template>
<div class="education center">
<div v-if="object.timelines != null">
<template v-for="(time,index) in object.timelines">
<p :key="index">{{ time.schoolyear }}</p>
<div :key="index" v-bind="time" v-for="(text,index) in time.text">
<p :key="text">Degree: {{ text.degree }}</p>
<p>Institution: {{ text.institution }}</p>
<p>Where: {{text.where}}</p>
</div>
</template>
</div>
</div>
</template>


<script>
export default {
el: ".education",
data: function() {
return {
object: {
timelines: [
{
schoolyear: "2016 - 2017",
text: [
{ degree: "Applied Computer Science" },
{ institution: "Thomas More University of Applied Science" },
{ where: "Belgium, Geel" }
]
},
{
schoolyear: "2018 - 2019",
text: [
{ degree: "Business IT" },
{ institution: "HAMK University of Applied Science" },
{ where: "Finland, Hämeenlinna" }
]
}
]
}
};
}
};
</script>


I only want to show text.degree once for schoolyear="2016 - 2017"










share|improve this question

























  • try <p :key="text" v-if="time.schoolyear=='2016 - 2017'">Degree: {{ text.degree }}</p>

    – Boussadjra Brahim
    Jan 19 at 23:56











  • That does unfortunately not fix the issue. Also seems like a huge amount of work if you are working with large arrays...

    – mdeconinck
    Jan 20 at 0:02











  • please rephrase your issue since i misunderstood you

    – Boussadjra Brahim
    Jan 20 at 0:04











  • Do you have control over the object/timeline?

    – Bergur
    Jan 20 at 0:22











  • i see that your text object should be like i did here

    – Boussadjra Brahim
    Jan 20 at 0:29
















0















I am trying to display the values of the text array/object but I am getting an output trying to show me paragraphs for every name inside the array/object.



Link to current result: current result



I am fairly new to vue.js so any tips are welcome!





<template>
<div class="education center">
<div v-if="object.timelines != null">
<template v-for="(time,index) in object.timelines">
<p :key="index">{{ time.schoolyear }}</p>
<div :key="index" v-bind="time" v-for="(text,index) in time.text">
<p :key="text">Degree: {{ text.degree }}</p>
<p>Institution: {{ text.institution }}</p>
<p>Where: {{text.where}}</p>
</div>
</template>
</div>
</div>
</template>


<script>
export default {
el: ".education",
data: function() {
return {
object: {
timelines: [
{
schoolyear: "2016 - 2017",
text: [
{ degree: "Applied Computer Science" },
{ institution: "Thomas More University of Applied Science" },
{ where: "Belgium, Geel" }
]
},
{
schoolyear: "2018 - 2019",
text: [
{ degree: "Business IT" },
{ institution: "HAMK University of Applied Science" },
{ where: "Finland, Hämeenlinna" }
]
}
]
}
};
}
};
</script>


I only want to show text.degree once for schoolyear="2016 - 2017"










share|improve this question

























  • try <p :key="text" v-if="time.schoolyear=='2016 - 2017'">Degree: {{ text.degree }}</p>

    – Boussadjra Brahim
    Jan 19 at 23:56











  • That does unfortunately not fix the issue. Also seems like a huge amount of work if you are working with large arrays...

    – mdeconinck
    Jan 20 at 0:02











  • please rephrase your issue since i misunderstood you

    – Boussadjra Brahim
    Jan 20 at 0:04











  • Do you have control over the object/timeline?

    – Bergur
    Jan 20 at 0:22











  • i see that your text object should be like i did here

    – Boussadjra Brahim
    Jan 20 at 0:29














0












0








0








I am trying to display the values of the text array/object but I am getting an output trying to show me paragraphs for every name inside the array/object.



Link to current result: current result



I am fairly new to vue.js so any tips are welcome!





<template>
<div class="education center">
<div v-if="object.timelines != null">
<template v-for="(time,index) in object.timelines">
<p :key="index">{{ time.schoolyear }}</p>
<div :key="index" v-bind="time" v-for="(text,index) in time.text">
<p :key="text">Degree: {{ text.degree }}</p>
<p>Institution: {{ text.institution }}</p>
<p>Where: {{text.where}}</p>
</div>
</template>
</div>
</div>
</template>


<script>
export default {
el: ".education",
data: function() {
return {
object: {
timelines: [
{
schoolyear: "2016 - 2017",
text: [
{ degree: "Applied Computer Science" },
{ institution: "Thomas More University of Applied Science" },
{ where: "Belgium, Geel" }
]
},
{
schoolyear: "2018 - 2019",
text: [
{ degree: "Business IT" },
{ institution: "HAMK University of Applied Science" },
{ where: "Finland, Hämeenlinna" }
]
}
]
}
};
}
};
</script>


I only want to show text.degree once for schoolyear="2016 - 2017"










share|improve this question
















I am trying to display the values of the text array/object but I am getting an output trying to show me paragraphs for every name inside the array/object.



Link to current result: current result



I am fairly new to vue.js so any tips are welcome!





<template>
<div class="education center">
<div v-if="object.timelines != null">
<template v-for="(time,index) in object.timelines">
<p :key="index">{{ time.schoolyear }}</p>
<div :key="index" v-bind="time" v-for="(text,index) in time.text">
<p :key="text">Degree: {{ text.degree }}</p>
<p>Institution: {{ text.institution }}</p>
<p>Where: {{text.where}}</p>
</div>
</template>
</div>
</div>
</template>


<script>
export default {
el: ".education",
data: function() {
return {
object: {
timelines: [
{
schoolyear: "2016 - 2017",
text: [
{ degree: "Applied Computer Science" },
{ institution: "Thomas More University of Applied Science" },
{ where: "Belgium, Geel" }
]
},
{
schoolyear: "2018 - 2019",
text: [
{ degree: "Business IT" },
{ institution: "HAMK University of Applied Science" },
{ where: "Finland, Hämeenlinna" }
]
}
]
}
};
}
};
</script>


I only want to show text.degree once for schoolyear="2016 - 2017"







arrays vue.js v-for






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 23:59







mdeconinck

















asked Jan 19 at 23:47









mdeconinckmdeconinck

11




11













  • try <p :key="text" v-if="time.schoolyear=='2016 - 2017'">Degree: {{ text.degree }}</p>

    – Boussadjra Brahim
    Jan 19 at 23:56











  • That does unfortunately not fix the issue. Also seems like a huge amount of work if you are working with large arrays...

    – mdeconinck
    Jan 20 at 0:02











  • please rephrase your issue since i misunderstood you

    – Boussadjra Brahim
    Jan 20 at 0:04











  • Do you have control over the object/timeline?

    – Bergur
    Jan 20 at 0:22











  • i see that your text object should be like i did here

    – Boussadjra Brahim
    Jan 20 at 0:29



















  • try <p :key="text" v-if="time.schoolyear=='2016 - 2017'">Degree: {{ text.degree }}</p>

    – Boussadjra Brahim
    Jan 19 at 23:56











  • That does unfortunately not fix the issue. Also seems like a huge amount of work if you are working with large arrays...

    – mdeconinck
    Jan 20 at 0:02











  • please rephrase your issue since i misunderstood you

    – Boussadjra Brahim
    Jan 20 at 0:04











  • Do you have control over the object/timeline?

    – Bergur
    Jan 20 at 0:22











  • i see that your text object should be like i did here

    – Boussadjra Brahim
    Jan 20 at 0:29

















try <p :key="text" v-if="time.schoolyear=='2016 - 2017'">Degree: {{ text.degree }}</p>

– Boussadjra Brahim
Jan 19 at 23:56





try <p :key="text" v-if="time.schoolyear=='2016 - 2017'">Degree: {{ text.degree }}</p>

– Boussadjra Brahim
Jan 19 at 23:56













That does unfortunately not fix the issue. Also seems like a huge amount of work if you are working with large arrays...

– mdeconinck
Jan 20 at 0:02





That does unfortunately not fix the issue. Also seems like a huge amount of work if you are working with large arrays...

– mdeconinck
Jan 20 at 0:02













please rephrase your issue since i misunderstood you

– Boussadjra Brahim
Jan 20 at 0:04





please rephrase your issue since i misunderstood you

– Boussadjra Brahim
Jan 20 at 0:04













Do you have control over the object/timeline?

– Bergur
Jan 20 at 0:22





Do you have control over the object/timeline?

– Bergur
Jan 20 at 0:22













i see that your text object should be like i did here

– Boussadjra Brahim
Jan 20 at 0:29





i see that your text object should be like i did here

– Boussadjra Brahim
Jan 20 at 0:29












2 Answers
2






active

oldest

votes


















0














It's not completely clear to me what you want, but maybe is it that you want to iterate through the .text array and, for each entry in the array, display both its key and its content. If so, you might try something like:



<p v-for="(entry, index) in time.text" :key="index">
{{Object.keys(entry)[0]}}: {{Object.values(entry)[0]}}
</p>


If you need to worry about title case for the keys, you could either use a computed property to convert the array, or define a method to convert each string.






share|improve this answer
























  • You could use a filter to uppercase the first letter of the keys

    – Boussadjra Brahim
    Jan 20 at 1:48



















0














First of all thanks to Boussadjra Brahim for providing the codepen wich resolved my issue.



I will first rephrase the question and then copy the solution.



The question: I want to print out values from an array inside a javascript object. In my <div>tag is is currently printing trying to print out text.institution for each element in the text array.



resulting in vue.js trying to show <p>Institution: {{ text.institution }}</p>
for degree, institution and where. Giving a browser output of:



<p>Degree:</p>



<p>Institution: Thomas More University of Applied Science"</p>



<p>Where:</p>



for text.where this would change to



<p>Degree:</p>



<p>Institution:</p>



<p>Where: Belgium, Geel</p>



The answer: Yet again a huge thanks to Boussadjra Brahim for showing the solution.





/* eslint-disable vue/require-v-for-key */
<template>
<div class="education center">
<div v-if="object.timelines != null">
<template v-for="(time,index) in object.timelines">
<p :key="index">{{ time.schoolyear }}</p>
<div :key="index" :set="text = time.text">
<p>Degree: {{ text.degree }}</p>
<p>Institution: {{ text.institution }}</p>
<p>Where: {{text.where}}</p>
</div>
</template>
</div>
</div>
</template>


<script>
export default {
el: ".education",
data(){
return {
object: {
timelines: [
{
schoolyear: "2016 - 2017",
text: {
degree: "Applied Computer Science",
institution: "Thomas More University of Applied Science",
where: "Belgium, Geel"
}
},
{
schoolyear: "2018 - 2019",
text: {
degree: "Business IT",
institution: "HAMK University of Applied Science",
where: "Finland, Hämeenlinna"
}
}
]
}
};
}
};
</script>


I changed the text array from square brackets to curly brackets and instead of using a v-for= I changed to a :set=.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272346%2fproper-way-to-show-array-inside-an-array-with-v-for%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's not completely clear to me what you want, but maybe is it that you want to iterate through the .text array and, for each entry in the array, display both its key and its content. If so, you might try something like:



    <p v-for="(entry, index) in time.text" :key="index">
    {{Object.keys(entry)[0]}}: {{Object.values(entry)[0]}}
    </p>


    If you need to worry about title case for the keys, you could either use a computed property to convert the array, or define a method to convert each string.






    share|improve this answer
























    • You could use a filter to uppercase the first letter of the keys

      – Boussadjra Brahim
      Jan 20 at 1:48
















    0














    It's not completely clear to me what you want, but maybe is it that you want to iterate through the .text array and, for each entry in the array, display both its key and its content. If so, you might try something like:



    <p v-for="(entry, index) in time.text" :key="index">
    {{Object.keys(entry)[0]}}: {{Object.values(entry)[0]}}
    </p>


    If you need to worry about title case for the keys, you could either use a computed property to convert the array, or define a method to convert each string.






    share|improve this answer
























    • You could use a filter to uppercase the first letter of the keys

      – Boussadjra Brahim
      Jan 20 at 1:48














    0












    0








    0







    It's not completely clear to me what you want, but maybe is it that you want to iterate through the .text array and, for each entry in the array, display both its key and its content. If so, you might try something like:



    <p v-for="(entry, index) in time.text" :key="index">
    {{Object.keys(entry)[0]}}: {{Object.values(entry)[0]}}
    </p>


    If you need to worry about title case for the keys, you could either use a computed property to convert the array, or define a method to convert each string.






    share|improve this answer













    It's not completely clear to me what you want, but maybe is it that you want to iterate through the .text array and, for each entry in the array, display both its key and its content. If so, you might try something like:



    <p v-for="(entry, index) in time.text" :key="index">
    {{Object.keys(entry)[0]}}: {{Object.values(entry)[0]}}
    </p>


    If you need to worry about title case for the keys, you could either use a computed property to convert the array, or define a method to convert each string.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 20 at 1:42









    Stephen ThomasStephen Thomas

    9,92621733




    9,92621733













    • You could use a filter to uppercase the first letter of the keys

      – Boussadjra Brahim
      Jan 20 at 1:48



















    • You could use a filter to uppercase the first letter of the keys

      – Boussadjra Brahim
      Jan 20 at 1:48

















    You could use a filter to uppercase the first letter of the keys

    – Boussadjra Brahim
    Jan 20 at 1:48





    You could use a filter to uppercase the first letter of the keys

    – Boussadjra Brahim
    Jan 20 at 1:48













    0














    First of all thanks to Boussadjra Brahim for providing the codepen wich resolved my issue.



    I will first rephrase the question and then copy the solution.



    The question: I want to print out values from an array inside a javascript object. In my <div>tag is is currently printing trying to print out text.institution for each element in the text array.



    resulting in vue.js trying to show <p>Institution: {{ text.institution }}</p>
    for degree, institution and where. Giving a browser output of:



    <p>Degree:</p>



    <p>Institution: Thomas More University of Applied Science"</p>



    <p>Where:</p>



    for text.where this would change to



    <p>Degree:</p>



    <p>Institution:</p>



    <p>Where: Belgium, Geel</p>



    The answer: Yet again a huge thanks to Boussadjra Brahim for showing the solution.





    /* eslint-disable vue/require-v-for-key */
    <template>
    <div class="education center">
    <div v-if="object.timelines != null">
    <template v-for="(time,index) in object.timelines">
    <p :key="index">{{ time.schoolyear }}</p>
    <div :key="index" :set="text = time.text">
    <p>Degree: {{ text.degree }}</p>
    <p>Institution: {{ text.institution }}</p>
    <p>Where: {{text.where}}</p>
    </div>
    </template>
    </div>
    </div>
    </template>


    <script>
    export default {
    el: ".education",
    data(){
    return {
    object: {
    timelines: [
    {
    schoolyear: "2016 - 2017",
    text: {
    degree: "Applied Computer Science",
    institution: "Thomas More University of Applied Science",
    where: "Belgium, Geel"
    }
    },
    {
    schoolyear: "2018 - 2019",
    text: {
    degree: "Business IT",
    institution: "HAMK University of Applied Science",
    where: "Finland, Hämeenlinna"
    }
    }
    ]
    }
    };
    }
    };
    </script>


    I changed the text array from square brackets to curly brackets and instead of using a v-for= I changed to a :set=.






    share|improve this answer




























      0














      First of all thanks to Boussadjra Brahim for providing the codepen wich resolved my issue.



      I will first rephrase the question and then copy the solution.



      The question: I want to print out values from an array inside a javascript object. In my <div>tag is is currently printing trying to print out text.institution for each element in the text array.



      resulting in vue.js trying to show <p>Institution: {{ text.institution }}</p>
      for degree, institution and where. Giving a browser output of:



      <p>Degree:</p>



      <p>Institution: Thomas More University of Applied Science"</p>



      <p>Where:</p>



      for text.where this would change to



      <p>Degree:</p>



      <p>Institution:</p>



      <p>Where: Belgium, Geel</p>



      The answer: Yet again a huge thanks to Boussadjra Brahim for showing the solution.





      /* eslint-disable vue/require-v-for-key */
      <template>
      <div class="education center">
      <div v-if="object.timelines != null">
      <template v-for="(time,index) in object.timelines">
      <p :key="index">{{ time.schoolyear }}</p>
      <div :key="index" :set="text = time.text">
      <p>Degree: {{ text.degree }}</p>
      <p>Institution: {{ text.institution }}</p>
      <p>Where: {{text.where}}</p>
      </div>
      </template>
      </div>
      </div>
      </template>


      <script>
      export default {
      el: ".education",
      data(){
      return {
      object: {
      timelines: [
      {
      schoolyear: "2016 - 2017",
      text: {
      degree: "Applied Computer Science",
      institution: "Thomas More University of Applied Science",
      where: "Belgium, Geel"
      }
      },
      {
      schoolyear: "2018 - 2019",
      text: {
      degree: "Business IT",
      institution: "HAMK University of Applied Science",
      where: "Finland, Hämeenlinna"
      }
      }
      ]
      }
      };
      }
      };
      </script>


      I changed the text array from square brackets to curly brackets and instead of using a v-for= I changed to a :set=.






      share|improve this answer


























        0












        0








        0







        First of all thanks to Boussadjra Brahim for providing the codepen wich resolved my issue.



        I will first rephrase the question and then copy the solution.



        The question: I want to print out values from an array inside a javascript object. In my <div>tag is is currently printing trying to print out text.institution for each element in the text array.



        resulting in vue.js trying to show <p>Institution: {{ text.institution }}</p>
        for degree, institution and where. Giving a browser output of:



        <p>Degree:</p>



        <p>Institution: Thomas More University of Applied Science"</p>



        <p>Where:</p>



        for text.where this would change to



        <p>Degree:</p>



        <p>Institution:</p>



        <p>Where: Belgium, Geel</p>



        The answer: Yet again a huge thanks to Boussadjra Brahim for showing the solution.





        /* eslint-disable vue/require-v-for-key */
        <template>
        <div class="education center">
        <div v-if="object.timelines != null">
        <template v-for="(time,index) in object.timelines">
        <p :key="index">{{ time.schoolyear }}</p>
        <div :key="index" :set="text = time.text">
        <p>Degree: {{ text.degree }}</p>
        <p>Institution: {{ text.institution }}</p>
        <p>Where: {{text.where}}</p>
        </div>
        </template>
        </div>
        </div>
        </template>


        <script>
        export default {
        el: ".education",
        data(){
        return {
        object: {
        timelines: [
        {
        schoolyear: "2016 - 2017",
        text: {
        degree: "Applied Computer Science",
        institution: "Thomas More University of Applied Science",
        where: "Belgium, Geel"
        }
        },
        {
        schoolyear: "2018 - 2019",
        text: {
        degree: "Business IT",
        institution: "HAMK University of Applied Science",
        where: "Finland, Hämeenlinna"
        }
        }
        ]
        }
        };
        }
        };
        </script>


        I changed the text array from square brackets to curly brackets and instead of using a v-for= I changed to a :set=.






        share|improve this answer













        First of all thanks to Boussadjra Brahim for providing the codepen wich resolved my issue.



        I will first rephrase the question and then copy the solution.



        The question: I want to print out values from an array inside a javascript object. In my <div>tag is is currently printing trying to print out text.institution for each element in the text array.



        resulting in vue.js trying to show <p>Institution: {{ text.institution }}</p>
        for degree, institution and where. Giving a browser output of:



        <p>Degree:</p>



        <p>Institution: Thomas More University of Applied Science"</p>



        <p>Where:</p>



        for text.where this would change to



        <p>Degree:</p>



        <p>Institution:</p>



        <p>Where: Belgium, Geel</p>



        The answer: Yet again a huge thanks to Boussadjra Brahim for showing the solution.





        /* eslint-disable vue/require-v-for-key */
        <template>
        <div class="education center">
        <div v-if="object.timelines != null">
        <template v-for="(time,index) in object.timelines">
        <p :key="index">{{ time.schoolyear }}</p>
        <div :key="index" :set="text = time.text">
        <p>Degree: {{ text.degree }}</p>
        <p>Institution: {{ text.institution }}</p>
        <p>Where: {{text.where}}</p>
        </div>
        </template>
        </div>
        </div>
        </template>


        <script>
        export default {
        el: ".education",
        data(){
        return {
        object: {
        timelines: [
        {
        schoolyear: "2016 - 2017",
        text: {
        degree: "Applied Computer Science",
        institution: "Thomas More University of Applied Science",
        where: "Belgium, Geel"
        }
        },
        {
        schoolyear: "2018 - 2019",
        text: {
        degree: "Business IT",
        institution: "HAMK University of Applied Science",
        where: "Finland, Hämeenlinna"
        }
        }
        ]
        }
        };
        }
        };
        </script>


        I changed the text array from square brackets to curly brackets and instead of using a v-for= I changed to a :set=.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 20 at 9:24









        mdeconinckmdeconinck

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272346%2fproper-way-to-show-array-inside-an-array-with-v-for%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre