jeudi 23 février 2017

vue.js grab a random element from array

I try to create a little tool for learning vue.js which should return the time it takes to go somewhere: Codepen

It works more or less for now but I would like to add a random value from each array on page load. How would I have to do this?

var App = new Vue({
  el: '#App',
  data: {
    locList: [
      { name: 'New York', distance: '100' },
      { name: 'Barcelona', distance: '500'  },
      { name: 'Hawaii', distance: '600' }
    ],
    vehicleList: [
      { name: 'Car', speed: '100' },
      { name: 'Bicycle', speed: '25' },
      { name: 'A pair of Shoes', speed: '5' }
    ],
    selectedVehicle: '0', // here I'd like to add a random speed
    selectedLocation: '0',// here I'd like to add a random distance
  },
  computed: {
    calculateThis: function () {
      return Math.abs(this.selectedLocation / this.selectedVehicle)
    }
  }
});

I tought about adding a another function into the computed section. But I had no luck:

randomNum: function () {
  // something like this? (It absolutely does not work)
  return Math.random() * data.locList.distance ), 0)
},




Aucun commentaire:

Enregistrer un commentaire