One simple approach is to just shuffle the array of indices and then use that to get both your arrays in the corresponding order:
const mp3 = ['sing.mp3','song.mp3','tune.mp3','jam.mp3'];
const ogg = ['sing.ogg','song.ogg','tune.ogg','jam.ogg'];
const indices = _.shuffle([0, 1, 2, 3]);
const shuffledMp3 = indices.map(idx => mp3[idx]);
const shuffledOgg = indices.map(idx => ogg[idx]);
console.log(shuffledMp3, shuffledOgg);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
However, you shouldn't need to do this. Your question only arises because you're storing the data in an unhelpful way. Since the elements of the two arrays are clearly linked to each other, you shouldn't store them as 2 arrays, but as a single array whose elements are objects holding both filenames as properties. This enables you to get what you want with a single shuffle:
const files = [{mp3: 'sing.mp3', ogg: 'sing.ogg'}, {mp3: 'song.mp3', ogg: 'song.ogg'}, {mp3: 'tune.mp3', ogg: 'tune.ogg'}, {mp3: 'jam.mp3', ogg: 'jam.ogg'}];
const shuffled = _.shuffle(files);
console.log(shuffled.map(file => file.mp3));
console.log(shuffled.map(file => file.ogg));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
(It's also worth noting that if all your file names are as predictably formed as in your example, even this is unnecessary - you can just store the "raw" filenames: ['sing', 'song', 'tune', 'jam'] and add whatever file extension you need at the point of needing it. But perhaps your real data is not so consistent.)
Javascript- Lodash shuffle vs. Math.Random() - Stack Overflow
Why does this shuffle not work in Vue?
Lodash shuffle is creating a copy of your array, shuffling it, and returning a shuffled copy which you then assign to this.cells, this lets Vue know that this.cells is a new array and that your view needs to be re-rendered.
However within shuffle2 you are mutating the array directly, then assigning the same array to itself, which I guess Vue does not detect as a change.
I managed to get the snippet you had with shuffle2 working by either calling this.$forceUpdate() after you assign the shuffled array to this.cells, or by creating a copy of the array within the shuffle2 method and returning that instead. You could possibly also use Vue.set() to change the values within the array if you wanna directly mutate the array within your method.
More on reddit.comHow to shuffle an array on route change on a global component
That sounds like a heavy solution, to be honest. Instead of shuffling the entire array, it would be better to use Math.floor(Math.random() * this.projects.length)
As for triggering on route change; look into adding a watcher for this.$route.path and see if that gets you where you need.
Ideally, you'd want something like:
data: {
return {
project: null,
projects: []
}
},
watch: {
$route() {
this.project = Math.floor(Math.random() * this.projects.length)
}
}
This is typed on my phone, so you'll need to test and tweak it a bit.
More on reddit.comHow can you randomize the order in which components are rendered when a user clicks on them? [help]
» npm install lodash.shuffle