Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptVue.js List Rendering v-for with an Object

Vue.js List Rendering v-for with an Object

Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries.

In order to repeat a task for a fixed amount of time, we make use of the for loop. The v-for is used to traverse over the object and display the required data as per the user’s choice. There are a lot of data that needs to be rendered on a web page. Sometimes the data comes in the form of an object. The key value is not double-quoted in the object.

Object : { key : "Geeks for Geeks" }

Syntax:

<li v-for="value in object">
   {{ value }}
</li>

Approach: Here, we will create a Vue project, and then we will create a different UI that displays object data.

Creating Vue Project:

Step 1: To create a Vue app you need to install Vue modules using this npm command. You need to make sure you have the node installed previously.

npm install vue

Step 2: Use Vue JS through CLI. Open your terminal or command prompt and run the below command.

npm install --global vue-cli

Step 3: Run the below command to create the project.

vue init webpack myproject

Step 4:  After creating your Vue project move into the folder to perform different operations.

cd myproject

Step to run the application: Open the terminal and type the following command.

npm run dev

Open your browser. Open a tab with localhost running (http://localhost:8080/) and you can see the output shown in the image.

Project Structure: After running the commands (mentioned in the above steps), if you open the project in an editor, you can see a similar project structure (as shown below).

Project Structure

Example: Running a loop to display all the keys and their values in the object along with the index. The first argument is the value then the key and the third is the index in v-for. 

App.vue




<template>
  <div id="app">
    <h1 style="color: green">neveropen</h1>
    <strong>
        Print all key , values of an object 
        along with index
    </strong><br />
    <p v-for="(value, key, index) in object">
      <strong>
          {{ index }}.   {{ key }}   - {{ value }}
      </strong>
    </p>
  
  </div>
</template>
  
<script>
  export default {
    name: "App",
    data() {
      return {
        object: {
          Site: " Geeks for Geeks ",
          Framework: " Vue ",
          Type: " Article ",
          Subject: " v-for with an Object ",
        },
      };
    },
  };
</script>


Output:

v-for with an object

Reference: https://v2.vuejs.org/v2/guide/list.html#v-for-with-an-Object

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments