Skip to main content
  1. About
  2. For Teams
Asked
Viewed 6k times
0

I need a nested v-for loop in Vuejs but have doubt about how to do it, how to organize my Data and the v-for loop.

Here is the Data. I would like to be able to iterate through modifiers, without the need to call precisely modifiers1, modifiers2.

The idea is that the first v-for loop with iterate through the modifiers object, and a nested v-for loop will iterate through the different blocks inside. Like this, I have an automatic nested v-for loop.

modifiers1: {
  block1: {
    class: 'doc_button--green',
    description: 'Primary Button'
  },
  block2: {
    class: 'doc_button--orange',
    description: 'Secondary Button'
  },
  block3: {
    class: 'doc_button--red',
    description: 'Tertiary Button'
  }
},
modifiers2: {
  block1: {
    class: 'doc_button--small',
    description: 'Small Button'
  },
  block2: {
    class: 'doc_button--big',
    description: 'Big Button'
  }
}

A simple example of loop I think about is:

<div v-for="(modifier) in modifiers" :key="modifier">
 <ul v-for="(block) in blocks" :key="block">
   <li></li>
 </ul>
</div>

Is it possible, and if yes how? Do I need to organize my Data into a nested array? Thanks

4
  • first, you'll need data called modifiers and blocks
    Jaromanda X
    –  Jaromanda X
    2019-12-11 09:14:51 +00:00
    Commented Dec 11, 2019 at 9:14
  • 1
    What is modifiers and blocks?
    Nitheesh
    –  Nitheesh
    2019-12-11 09:16:16 +00:00
    Commented Dec 11, 2019 at 9:16
  • @JaromandaX This is an example. I tried different stuff and it didn't work. This is the nested loop I am thinking about. But don't know how to organize my Data to get this.
    Grégory Huyghe
    –  Grégory Huyghe
    2019-12-11 09:24:33 +00:00
    Commented Dec 11, 2019 at 9:24
  • well, the loop is wrong, since blocks has no relation to modifiers
    Jaromanda X
    –  Jaromanda X
    2019-12-11 09:27:46 +00:00
    Commented Dec 11, 2019 at 9:27

3 Answers 3

3

Assuming that your data structure is stored in the variable modifiers, you would just need to adjust your second v-for to loop over the modifier variable from the first v-for.

In effect your code would become this (expanded to highlight more ways to use the data from the loops):

let modifiers = {
  modifier1: {
    block1: {
      class: 'doc_button--green',
      description: 'Primary Button'
    },
    block2: {
      class: 'doc_button--orange',
      description: 'Secondary Button'
    },
    block3: {
      class: 'doc_button--red',
      description: 'Tertiary Button'
    }
  },
  modifier2: {
    block1: {
      class: 'doc_button--small',
      description: 'Small Button'
    },
    block2: {
      class: 'doc_button--big',
      description: 'Big Button'
    }
  }
}
<div v-for="(blocks, modifier) in modifiers" :key="modifier">
  <ul v-for="(block, name) in blocks" :key="name">
    <li v-for="(value, key) in block" :key="key">{{key}}: {{value}}</li>
  </ul>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

One way would be to create a computed property to self reference the $data...

computed:{
    myData() {
        return this.$data
    }
},

Then in the template...

<div v-for="(modifier) in myData" :key="modifier">
    <ul v-for="(block,k) in modifier" :key="k">
        <li>{{ block.description }}</li>
    </ul>
</div>

Demo

Comments

0

You could also transform the Object into an array of modifiers, each with a nested array of blocks:

const data = {
  modifiers1: {
    block1: {
      class: 'doc_button--green',
      description: 'Primary Button'
    },
    block2: {
      class: 'doc_button--orange',
      description: 'Secondary Button'
    },
    block3: {
      class: 'doc_button--red',
      description: 'Tertiary Button'
    }
  },
  modifiers2: {
    block1: {
      class: 'doc_button--small',
      description: 'Small Button'
    },
    block2: {
      class: 'doc_button--big',
      description: 'Big Button'
    }
  }
}
const modifiers = Object.keys(data).map(modifierName => ({
  name: modifierName,
  blocks: Object.keys(data[modifierName]).map(blockName => ({
    ...data[modifierName][blockName],
    name: blockName
  }))
}));


console.log(modifiers);

Then you can loop over the nested array like so:

<div v-for="(modifier) in modifiers" :key="modifier.name">
 <ul v-for="(block) in modifier.blocks" :key="block.name">
   <li></li>
 </ul>
</div>

Comments

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.