Creating a for loop in Vue using v-for that begins at 0 and displays every third index, including the first index

Is there a way to display this code using Vuejs and v-for?

I'm interested in displaying data beginning with index 0, followed by every third index up to index 15. The total length of the data is 16. For example, like this: 0,3,6,9,12,15

for(i = 0; i < data.length; i+=3) {}

Answer №1

To optimize the data, you can preprocess it in the computed property:

new Vue({
  el: "#app",
  data() {
    return {
      items: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
    }
  },
  computed: {
    everyThird: function() {
      const thirds = []
      for(let i = 0; i < this.items.length; i+=3) {
        thirds.push(this.items[i])
      }
      return thirds;
    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <li v-for="(item, i) in everyThird" :key="i">
    {{ item }}
  </li>
</div>

Answer №2

If you want to filter elements in an array based on a specific condition, you can achieve this by using a computed property. Create an array within the computed property that only includes elements with indexes divisible by 3. Then, utilize this filtered array in your v-for loop. Below is an example of how you can implement this in your Vue.js component:

<div>
  <li v-for="(item, index) in elementInThirdPlace" :key="index">
    {{ item }}
  </li></div>

export default {
 data () { 
     return { items: [1,2,3,4,5,6,6] } 
  },
  computed: {
   elementInThirdPlace () {
    return this.items.filter((element, index) => index % 3 == 0 )
   }
 }
}

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to ensure that the $window.location.reload(true) function is executed only once in an Ionic/Angular application

I need help modifying this code so that it resets my app only once when it goes to the homepage instead of continuously reloading. What changes can I make in the JavaScript to achieve this? .controller('HomeCtrl', function ($scope, $window) { $s ...

Unable to update the information within a DIV using Python Django, as well as the webpage following a jQuery action

Exploring new ideas through the lens of two variables in an HTML template, messages and users, a series of buttons trigger a jQuery function upon being clicked. This function sends a post request to a Django server, which then returns an update to the mess ...

Modify the element within the Vuex store array upon modification of the component

Within my Vuex store, there exists an array of Notes, with each individual Note being represented by a <textarea>. I have a component called NoteArray that is responsible for displaying each Note: // NoteArray.vue export default { name: "NoteA ...

Activating a tab using a JS call in Bootstrap with the data-toggle attribute

My JSP page is using bootstrap's data-toggle="tab" functionality to display tabs. When the page loads, one tab is made active by default. <ul class="nav st-nav-tabs"> <li class="active"><a href="#tab1" data-toggle="tab">First Ta ...

Encountered an error: Unable to access property '0' of an undefined variable

Below is the script I am using: <script> var count; var obj; function search() { xhr = new XMLHttpRequest(); xhr.open("GET", "test.json", true); xhr.send(null); xhr.onreadystatechange = function() { if (xhr.readyState == 4 & ...

"Transferring a Ruby hash containing objects as keys to JavaScript on the front end as a Map object: a step-by-step

While Javascript does not natively support objects as object keys, the Map object offers a solution. My query is, how can an object like this be transmitted easily from the backend to the frontend? def test_controller object = {"a"=>1," ...

Disallow AJAX from sending files as strings

In my this.form.imagesFile variable, I have a file stored. Here is the link to the file: https://i.sstatic.net/waLPw.png I am looking to send this file using FormData and AJAX. Just so you know, I am working with Vue and Laravel. let getImg = []; ...

Angular pagination with enforced ellipses using Bootstrap

I have encountered an issue with pagination on the Plnkr I created. You can check out the problem here: http://plnkr.co/edit/YQAUCg5vjcgS1BEGB4nY?p=preview. Essentially, I have a large number of pages and I have set the attribute force-ellipses = true to ...

How to automatically open JQuery Accordion panels when the page loads

My Accordion loads with the 1st panel open upon page load, but I am looking for a way to have the entire Accordion closed by default. Any help or suggestions on how to achieve this would be highly appreciated. Thank you for your assistance. FIDDLE http:// ...

Is there a way to dynamically change the value within a text input field using vue.js 2?

Exploring my Laravel Blade view: <form slot="search" class="navbar-search" action="{{url('search')}}"> <search-header-view></search-header-view> </form> The Laravel Blade view calls a Vue component (search-header-vie ...

Unlocking the secret to accessing state in a JavaScript file using VUEX

Can anyone help me figure out why I can't access the currentUser property from the state in my vuex store's user.js file? I'm trying to use it in auth.js, but when I write: store.state.currentUser.uid === ... This is the error message I&apo ...

What could be causing the invalid_client error in response to this POST request I am making?

I am currently trying to establish a connection with an external API that has specific specifications: Host: name.of.host Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Through the utilization of the request module from npm, I am ...

What is the best way to iterate over each character in a string and trigger a function in JavaScript?

I am currently working on a project to create a random password generator. The code responsible for generating the password is functioning correctly. However, I am facing an issue with converting the characters of the password into phonetic equivalents. I ...

JavaScript - Populating Dropdown Menu with Fresh Information

I'm attempting to populate a combobox (input) with data fetched via AJAX. The goal is to display every city associated with a selected state, chosen from another select control (the state control). My Attempt: I've implemented the "change" even ...

Testing the Compatibility of Laravel 5.5 and Vue.js Blade Technology

In one of my blade files, I've included the following code snippet... <div> <contact-form></contact-form> </div> I am trying to verify that the Vue.js component is consistently mounted in my tests... public function testRo ...

Guide on how to retrieve a list of objects from a controller and showcase them utilizing JQuery in a Spring MVC application with ajax functionality

Here is the controller code snippet: @Controller public class MainController { @Autowired SqlSession sqlSession; @Autowired Message messageVO; @RequestMapping(value="getMessages", method=RequestMethod.GET) public @ResponseBody List<Messag ...

"HTML and JavaScript: The search functionality fails when trying to search for a string that is sourced from an array

Could someone assist me with a comparison issue I'm encountering? I am trying to use the includes() method to compare a list in a table with a list of names stored in a string. Strangely, when I hardcode the string directly into the includes() method, ...

validate that the input contains only numbers within the range of 5 to 60

I need to validate inputted numbers within the range of 5-60. <div class="col-md-3"> <input type="text" name="gd_time" id="gd_time" placeholder="Enter a number between 5 and 60 " class="form-control"> </div> The script I have attemp ...

Creating a captivating animation for a social media like button with CSS

I came across some animation code on the web and noticed that when I click the image, it replays the animation from the starting point. I want to make it so that when I click the image, the animation plays and stops, and if clicked again, resets the image. ...

Utilize the precise Kendo chart library files rather than relying on the kendo.all.min.js file

My application currently uses the Kendo chart, and for this purpose, it utilizes the "kendo.all.min.js" file which is quite large at 2.5 MB. To optimize the speed performance of the application, I decided to only include specific Kendo chart libraries. In ...