mercredi 28 mars 2018

Randomize JavaScript Array of Objects Based on Certain Key

I have a JavaScript array similar to this:

arrTest = [
  {qId: 1, text:"test a", order: 1},
  {qID: 2, text:"test b", order: 2},
  {qID: 3, text:"test c", order: 3},
  {qID: 4, text:"test d", order: 4}
];

I would like to randomize just the order key so that all other keys remain the same and the original order of the array stays as is:

arrTest = [
  {qId: 1, text:"test a", order: 3},
  {qID: 2, text:"test b", order: 1},
  {qID: 3, text:"test c", order: 4},
  {qID: 4, text:"test d", order: 2}
];

I have found lots or randomize array discussions and functions, but can't seem to find one that targets a specific key and leaves everything else the same.

Any assistance is appreciated.

Thank you!




Aucun commentaire:

Enregistrer un commentaire