Two Separate Arrays and V-For Loop and V-IF in Vue JS

Mr JJ

I am working on a project using Vue JS. This question might be duplicated from other similar questions but honestly, answer of similar questions did not help me to solve this issue!
There are two arrays (separate arrays). One array for singers which store information about each singer such as singer id, singer name and etc.
One array for musics which stores information about music such as music id, music name, singer id and etc.
Now I want to loop through both arrays and compare singer id in both arrays to get related music from musics array using v-for. I used all suggested v-for loop for arrays and nested arrays but it didn't work properly and I receive nothing or an error like: Cannot read property 'singer_id' of undefined.
I tried to solve this issue using regular nested v-for and a v-if:

//Singers Array
artists:[
      {
        singer_id: '1',
        category: 'artist',
        artist_name: 'Ariana Grande',
        pic: encodeURIComponent(require('@/assets/Items/Singers/Ariana Granade/Covers/HomeCover.jpeg')),
        followers: '57932090'
      },
      {
        singer_id: '2',
        category: 'artist',
        artist_name: 'Taylor Swift',
        pic: encodeURIComponent(require('@/assets/Items/Singers/Taylor Swift/HomeCover.jpg')),
        followers: '41100000'
      },
      {
        singer_id: '3',
        category: 'artist',
        artist_name: 'Eminem',
        pic: encodeURIComponent(require('@/assets/Items/Singers/Eminem/HomeCover.jpg')),
        followers: '36900000'
      },
]
//Musics Array
musics:[
      {
        song_id: '1',
        singer_id: '2',
        song_name: 'Blank Space',
        duration: '00:20',
        cover: encodeURIComponent(require('@/assets/Items/Singers/Taylor Swift/Blank Space.jpg')),
        song: encodeURIComponent(require('@/assets/Items/Singers/Taylor Swift/Taylor Swift - Blank Space.mp3'))
      },
      {
        song_id: '2',
        singer_id: '2',
        song_name: 'Delicate',
        duration: '00:20',
        cover: encodeURIComponent(require('@/assets/Items/Singers/Taylor Swift/Delicate.jpg')),
        song: encodeURIComponent(require('@/assets/Items/Singers/Taylor Swift/Taylor Swift - Delicate.mp3'))
      },
      {
        song_id: '3',
        singer_id: '3',
        song_name: 'Not Afraid',
        duration: '00:20',
        cover: encodeURIComponent(require('@/assets/Items/Singers/Eminem/not afraid.jpg')),
        song: encodeURIComponent(require('@/assets/Items/Singers/Eminem/Eminem - Not Afraid (Teaser).mp3'))
      }
]
//Computing Arrays
export default{
 computed:{
  GetArtists:function(){
    return this.$store.state.artists;
  },
  GetMusics:function(){
    return this.$store.state.musics;
  }
 }
}
<div class="fav_item_rows">
   <div class="fav_item_box" v-for="(songs,i) in GetMusics" :key="i">
       <router-link to="/" v-for="(artists,j) in GetArtists" :key="j">
             <div class="fav_item" v-if="songs[i]['singer_id'] == artists[j]['singer_id']">
               {{songs[i]}}
             </div>
       </router-link>
   </div>
</div>

In this case I receive mentioned error at the top. I also remove [i] and just calling singer_id but it didn't work too.
If someone helps with how to loop through these arrays and using v-for compare singer ids I really appreciate it.

Amaarrockz

I have done some changes ..check the below code

<div class="fav_item_rows">
   <div class="fav_item_box" v-for="(artist,i) in GetArtists" :key="i">
       <router-link to="/" v-for="(song,j) in GetMusics(artist.singer_id)" :key="j">
             <div class="fav_item">
               {{song}}
             </div>
       </router-link>
   </div>
</div>

And then replace your computed property GetMusic like below

computed:{
  GetArtists:function(){
    return this.$store.state.artists;
  },
  GetMusics:function(){
    return id =>  this.$store.state.musics.filter(song => song.singer_id === id);
  }
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I match the union of two arrays in a v-for loop

From Dev

vue js v-for loop display item only once

From Dev

how to get v-for loop data from outside of loop also in vue.js

From Dev

Vue v-model binded html element fails to update Vue instance when a separate JS entity binds and then changes the data

From Dev

Html, Vue, Laravel - loop in v-for and table

From Dev

v-for loop items not immediately updating in Vue

From Dev

With Vue.js, How to use a modal component inside v-for loop?

From Dev

In Vue.js app; how to add 'active' class to button in v-for loop

From Dev

Vue.js - Values created in a method (on-click) not being displayed in a v-for loop

From Dev

How to make a v-for loop of DIVs and show them by part (Vue.js)

From Dev

Vue.js v-for generated html block capturing collapse state with two way data binding

From Dev

function call within a vue js v-for directive makes the loop run into an infinite loop and I don't know why

From Dev

vue.js double v-for in list

From Dev

Vue JS – Using v-if with components

From Dev

Vue.js v-for not working in the application

From Dev

vue js select boxes in v-for

From Dev

vue.js v-link not working

From Dev

Vue.js v2.0 delimeters

From Dev

Vue JS v-for not iterating over array

From Dev

In Vue.js, is this a wrong writing for v-on?

From Dev

Vue multiple v-for loop ... is there any other way

From Dev

Vue loop using v-for then display only if no condition met

From Dev

Using two arrays in for loop

From Dev

foreach loop with two arrays

From Dev

For loop run in two arrays

From Dev

Passing arguments to a function: a two element tuple v. two separate arguments?

From Dev

Passing arguments to a function: a two element tuple v. two separate arguments?

From Dev

OpenMDAO v0.13: connecting slices of an nxm array in an assembly to 1xm arrays in n separate components

From Dev

D3.js v5 concat dynamic arrays

Related Related

  1. 1

    How do I match the union of two arrays in a v-for loop

  2. 2

    vue js v-for loop display item only once

  3. 3

    how to get v-for loop data from outside of loop also in vue.js

  4. 4

    Vue v-model binded html element fails to update Vue instance when a separate JS entity binds and then changes the data

  5. 5

    Html, Vue, Laravel - loop in v-for and table

  6. 6

    v-for loop items not immediately updating in Vue

  7. 7

    With Vue.js, How to use a modal component inside v-for loop?

  8. 8

    In Vue.js app; how to add 'active' class to button in v-for loop

  9. 9

    Vue.js - Values created in a method (on-click) not being displayed in a v-for loop

  10. 10

    How to make a v-for loop of DIVs and show them by part (Vue.js)

  11. 11

    Vue.js v-for generated html block capturing collapse state with two way data binding

  12. 12

    function call within a vue js v-for directive makes the loop run into an infinite loop and I don't know why

  13. 13

    vue.js double v-for in list

  14. 14

    Vue JS – Using v-if with components

  15. 15

    Vue.js v-for not working in the application

  16. 16

    vue js select boxes in v-for

  17. 17

    vue.js v-link not working

  18. 18

    Vue.js v2.0 delimeters

  19. 19

    Vue JS v-for not iterating over array

  20. 20

    In Vue.js, is this a wrong writing for v-on?

  21. 21

    Vue multiple v-for loop ... is there any other way

  22. 22

    Vue loop using v-for then display only if no condition met

  23. 23

    Using two arrays in for loop

  24. 24

    foreach loop with two arrays

  25. 25

    For loop run in two arrays

  26. 26

    Passing arguments to a function: a two element tuple v. two separate arguments?

  27. 27

    Passing arguments to a function: a two element tuple v. two separate arguments?

  28. 28

    OpenMDAO v0.13: connecting slices of an nxm array in an assembly to 1xm arrays in n separate components

  29. 29

    D3.js v5 concat dynamic arrays

HotTag

Archive