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.)

Answer from Robin Zigmond on Stack Overflow
🌐
Lodash
lodash.com › docs
Lodash Documentation
Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.
Top answer
1 of 1
3

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.)

Discussions

Javascript- Lodash shuffle vs. Math.Random() - Stack Overflow
I'm in the process of coding a simple BlackJack game in Javascript. So far, I have an array like this: var deckArray = [ "card1", "card2",...,"card52" ] I have a "deal" function set up like this... More on stackoverflow.com
🌐 stackoverflow.com
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.com
🌐 r/vuejs
2
2
December 14, 2017
How 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.com
🌐 r/vuejs
3
0
November 16, 2018
How can you randomize the order in which components are rendered when a user clicks on them? [help]
I’m not at my computer so I’ll try to vaguely help from my phone. You make an array called cardArr and put all your cards in there. Then you put it into your component like {cardArr[math.random.floor(cardArr.length - 1)]}. You will have to figure out how to make sure there’s no repeats. Just google “JavaScript random index of array no repeats” or something. And my syntax might be wrong. Like I said I’m on my phone. But you need the random number to be rounded down to a whole number to work as an index. More on reddit.com
🌐 r/reactjs
13
5
October 10, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › lodash-_-shuffle-method
Lodash _.shuffle() Method - GeeksforGeeks
September 3, 2024 - Lodash _.shuffle() method creates an array of shuffled values from the given collection using a version of the Fisher-Yates shuffle algorithm.
🌐
npm
npmjs.com › package › lodash.shuffle
lodash.shuffle - npm
August 13, 2016 - The lodash method `_.shuffle` exported as a module.. Latest version: 4.2.0, last published: 10 years ago. Start using lodash.shuffle in your project by running `npm i lodash.shuffle`. There are 145 other projects in the npm registry using lodash.shuffle.
      » npm install lodash.shuffle
    
Published   Aug 13, 2016
Version   4.2.0
Author   John-David Dalton
🌐
TutorialsPoint
tutorialspoint.com › home › lodash › lodash shuffle method
Lodash - shuffle method
January 20, 2021 - collection (Array|Object) − The collection to shuffle. ... var _ = require('lodash'); var list = [1, 2, 3, 4, 5] var result = _.shuffle(list); console.log(result);
🌐
Docs-lodash
docs-lodash.com › v4 › shuffle
_.shuffle – Lodash Docs v4.17.11
Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.
🌐
Lodash
lodash.info › doc › shuffle
shuffle - Lodash documentation
Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.
Find elsewhere
🌐
GitHub
github.com › lodash › lodash › issues › 28
Make `_.shuffle` work on objects. · Issue #28 · lodash/lodash
Underscore just fixed their _.shuffle to work with objects because internally they were using _.each and unintended object usage slipped in. This is kinda lame as it complicates things but I am wondering do you all think Lo-Dash should support this untested feature yet?
🌐
Tabnine
tabnine.com › home page › code › javascript › lodash
lodash.shuffle JavaScript and Node.js code examples | Tabnine
function getShuffledColorPalettesKeys() { return [...shuffle(Object.keys(colorPalettes))]; } LoDashStatic.map · Creates an array of values by running each element in collection through iteratee.
🌐
Stack Abuse
stackabuse.com › shuffling-arrays-in-javascript
Shuffling Arrays in JavaScript
August 26, 2023 - This will output a shuffled ... properly. If you're using Lodash, a popular JavaScript utility library, you can shuffle arrays with the _.shuffle() function....
🌐
Crio
crio.do › blog › how-to-shuffle-array-in-javascript-2025-criodo
How to Shuffle an Array in JavaScript?
December 15, 2024 - In-place Shuffling: The Fisher-Yates algorithm modifies the original array, avoiding extra memory usage. Unbiased Randomization: Ensures all permutations of the array are equally likely. Using a Utility Library: Lodash provides a _.shuffle method.
🌐
Netlify
netlify.com › blog › 2017 › 01 › 12 › common-javascript-functions-with-lodash
Common JavaScript functions with Lodash | Netlify
I am going to briefly go over some of my favorite functions in Lodash while approaching some common situations in JavaScript. Take a common task like Shuffling, not the popularize dance from the one-hit-wonders LMFAO, but the practice of randomizing an array of data.
🌐
Byby
byby.dev › js-array-shuffle
How to shuffle an array in JavaScript
One of the functions provided by Lodash is _.shuffle(), which is specifically designed to shuffle the elements of an array.
🌐
CodeSandbox
codesandbox.io › examples › package › lodash.shuffle
lodash.shuffle examples - CodeSandbox
Use this online lodash.shuffle playground to view and fork lodash.shuffle example apps and templates on CodeSandbox.
🌐
Examplejavascript
examplejavascript.com › lodash › shuffle
How to use the shuffle function from lodash
lodash.shuffle takes an array as input and returns a new array with the same elements, but in a shuffled order.
🌐
Favtutor
favtutor.com › articles › shuffle-an-array-javascript
How to Shuffle an Array in JavaScript? (with code)
January 31, 2024 - In this article, we learned how to shuffle an array in JavaScript. We can use the Fisher-Yates Algorithm, Drustenfield Algorithm, or libraries like Underscore or Lodash to shuffle an array.
🌐
Sling Academy
slingacademy.com › article › ways-to-shuffle-an-array-in-javascript
3 Ways to Shuffle an Array in JavaScript - Sling Academy
February 19, 2023 - const arr = ['dog', 'cat', 'dragon', 'slingacademy.com', 'banana']; // define a function that can be reused const shuffleArray = (array) => { // create a copy of the array so that the original array is not mutated const newArray = [...array]; for (let i = newArray.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [newArray[i], newArray[j]] = [newArray[j], newArray[i]]; } return newArray; }; // use the function const shuffledArray = shuffleArray(arr); console.log(shuffledArray); ... Lodash is a super popular multi-purpose Javascript open-source library.
🌐
TutorialsPoint
tutorialspoint.com › how-to-shuffle-an-array-in-a-random-manner-in-javascript
How to shuffle an array in a random manner in JavaScript?
This function accepts the single arguments list. the argument is used to hold the list of items when we will be shuffled. Return Value ? The returned value is an all-new randomized array holding all the elements which are available in the original array as proceed to the _.shuffle() function.
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 6563 › 0 › lodash-shuffle
Benchmark: lodash shuffle - MeasureThat.net
Lodash sort vs array.prototype.sort (strings) Lodash sort vs array.prototype.sort (strings) small · Lodash sort vs array.prototype.sort (larger array) Lodash sort vs array.prototype.sort (Lodash 4.17.15) Lodash sort vs array.prototype.sort fork for spread ·