0

I have an array of objects, and I'd like to randomize the order of the elements.

So I have the following:

const animals  = data?.fetchAllAnimals;
let randomizeAnimals = animals.sort(()=> Math.random() -0.5 )

But I keep getting this error in firefox: TypeError: 0 is read-only

and a different error in chrome: TypeError: Cannot assign to read only property '0' of object '[object Array]'

What am I doing wrong here?

3
  • 1
    stackoverflow.com/questions/64957735/… Commented Mar 9, 2022 at 18:00
  • It seems that the array has been frozen (or something similar) so that it cannot be changed. So enzou's solution should work. Commented Mar 9, 2022 at 19:14
  • Yes that worked! I just did let randomizedAnimals = [...animals]; randomizedAnimals.sort(() => Math.random() - 0.5); and everything is working okay now Commented Mar 9, 2022 at 20:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.