Search code examples
vue.jsvuedraggable

Vue access nested array objects


As my array consists of multiple data, it was difficult to upload all of them. So I have uploaded an image of my array and objects below.

Array Nested Object

View

<draggable :list="reservation.Reservation_people" class="list-group" draggable=".item" group="a">
   <div class="list-group-item item" v-for="element in reservation.Reservation_people" :key="element.name">
     <p>{{element.Person.first_name}}</p> /** Prints out Person.first_name **/

     /** is there a way to fetch Player_minors first_name="Sanu" as a separate entity 
under element.Person.first_name. So that it can drag Person.first_name and 
Player_minors.first_name separately; **/

    </div>
</draggable>

Is there a way to fetch Player_minors first_name="Sanu" as a separate entity under element.Person.first_name. So that it can drag Person.first_name and Player_minors.first_name separately?

Using element.Person.first_name prints out San. But if I use {{element.Person.Player.Player_minors.first_name}} it does not print out anything.


Solution

  • Player.player_minors is an array, so you can't do Player.player_minors.first_name.

    If you want to print the first player_minor name, you could do

    {{ element.Person.Player.player_minors[0].first_name }}
    

    If you want to print all of the player_minor names, you would have to use a nested v-for.

    <div class="list-group-item item" v-for="element in reservation.Reservation_people" :key="element.name">
         <p>{{element.Person.first_name}}</p> /** Prints out Person.first_name **/
    
         <p
            v-for="player_minor in element.Person.Player.player_minors" 
            :key="player_minor.id">
              {{player_minor.first_name}}
         </p>
    
    </div>