0

I am trying to shuffle an array of objects using an shuffle npm module (https://www.npmjs.com/package/shuffle-array) - However I am getting errors like 'ypeError: Object(...) is not a function'.

   let posts = [
   {
    postUrl: "https://www.linkedin.com/1",
    action: "Post",
    profileUrl: "https://www.linkedin.com/company/1",
    timestamp: "2019-10-09T09:40:05.356Z"
    },
    {
    postUrl: "https://www.linkedin.com/2",
    action: "Post",
    profileUrl: "https://www.linkedin.com/company/2",
    timestamp: "2019-10-09T09:40:05.356Z"
    },
    {
    postUrl: "https://www.linkedin.com/3",
    action: "Post",
    profileUrl: "https://www.linkedin.com/company/3",
    timestamp: "2019-10-09T09:40:05.356Z"
    },
]

posts = shuffle(posts);

Can you see where I am going wrong? or how else to shuffle these objects?

BennKingy
  • 1,265
  • 1
  • 18
  • 43
  • There are plenty of vanilla javascript options. Do you need help with this specific package or are you okay with other options? – adiga Oct 09 '19 at 15:04
  • Please englighten me dude :) – BennKingy Oct 09 '19 at 15:05
  • 2
    [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954) and [hundreds of others](https://stackoverflow.com/questions/linked/2450954?sort=votes) – adiga Oct 09 '19 at 15:05
  • The code fragment you posted worked for me. Can you try posting an entire file which gives you this error? – Oli Oct 09 '19 at 15:12

3 Answers3

2

For most uses, the best way to go about this is to use the Fisher Yates / Knuth shuffle, as I'm sure a few other people will link to. It's discussed in this article:

How to randomize (shuffle) a JavaScript array?

The meat of the randomisation boils down to the following (also found on the aforementioned post!):

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

// Used like so
var arr = [2, 11, 37, 42];
arr = shuffle(arr);
console.log(arr);

Hope this has been helpful!

0

You can use the Math function random() to read and assign the indexes in the array. An example might be:

let thisarray = ["Hi","Hope","This","Helps!"];

var thisarrayprime = [];

for (let i = 0; i < thisarray.length; i++) {


   var newarray = Math.floor(Math.random() * thisarray.length);

   thisarrayprime[new] = thisarray[i];

   thisarray.splice((i-1), 1);

}

Hope this helps!

Vendetta
  • 2,078
  • 3
  • 13
  • 31
0

I tried to run your code online. It is working fine.
code is written here https://repl.it/repls/ClosedUnevenDevelopments
I think there is some issue with your project setting. look at this thread https://github.com/facebook/react/issues/14484 .

stormForce
  • 86
  • 6