I want to shuffle objects inside an object and I found this code, but unfortunately it doesn't work and I get the same order as the object is written!
How can I get shuffled result?
const obj = {
0: {
expands: [0],
},
1: {
expands: [1],
},
2: {
reference: "Have you ever* looked* us",
},
};
function shuffleObject(obj){
// new obj to return
let newObj = {};
// create keys array
var keys = Object.keys(obj);
// randomize keys array
keys.sort(function(a,b){return Math.random()- 0.5;});
// save in new array
keys.forEach(function(k) {
newObj[k] = obj[k];
});
return newObj;
}
console.log(shuffleObject(obj));