JavaScript - 在对象内打乱对象(随机化)

发布于 2024-09-19 15:55:21 字数 564 浏览 3 评论 0原文

我需要根据 JSON 结果实现随机化。

JSON 的格式是两个对象:

result:

Question(object)

[Object { id="4c6e9a41470b19_96235904",  more...}, 
 Object { id="4c784e6e928868_58699409",  more...}, 
 Object { id="4c6ecd074662c5_02703822",  more...}, 6 more...]

Topic(object)

[Object { id="3jhf3533279827_23424234",  more...}, 
 Object { id="4634663466cvv5_43235236",  more...}, 
 Object { id="47hf3892735298_08476548",  more...}, 2 more...]

我想随机化问题对象和主题对象内对象的顺序。

I need to implement a randomization from JSON result.

The format of the JSON is two objects:

result:

Question(object)

[Object { id="4c6e9a41470b19_96235904",  more...}, 
 Object { id="4c784e6e928868_58699409",  more...}, 
 Object { id="4c6ecd074662c5_02703822",  more...}, 6 more...]

Topic(object)

[Object { id="3jhf3533279827_23424234",  more...}, 
 Object { id="4634663466cvv5_43235236",  more...}, 
 Object { id="47hf3892735298_08476548",  more...}, 2 more...]

I want to randomize the order of the objects inside the question object and the topic objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疑心病 2024-09-26 15:55:21

您可以使用Fisher-Yates-Durstenfeld shuffle

var shuffledQuestionArray = shuffle(yourQuestionArray);
var shuffledTopicArray = shuffle(yourTopicArray);

// ...

function shuffle(sourceArray) {
    for (var i = 0; i < sourceArray.length - 1; i++) {
        var j = i + Math.floor(Math.random() * (sourceArray.length - i));

        var temp = sourceArray[j];
        sourceArray[j] = sourceArray[i];
        sourceArray[i] = temp;
    }
    return sourceArray;
}

You could use a Fisher-Yates-Durstenfeld shuffle:

var shuffledQuestionArray = shuffle(yourQuestionArray);
var shuffledTopicArray = shuffle(yourTopicArray);

// ...

function shuffle(sourceArray) {
    for (var i = 0; i < sourceArray.length - 1; i++) {
        var j = i + Math.floor(Math.random() * (sourceArray.length - i));

        var temp = sourceArray[j];
        sourceArray[j] = sourceArray[i];
        sourceArray[i] = temp;
    }
    return sourceArray;
}
愁以何悠 2024-09-26 15:55:21

最简单的方法(不是完美的随机播放,但在某些情况下可能更好):

function randomize(a, b) {
    return Math.random() - 0.5;
}

yourQuestionArray.sort(randomize);
yourTopicArray.sort(randomize);

yourQuestionArray.sort(function (a, b) {return Math.random() - 0.5;});
yourTopicArray.sort(function (a, b) {return Math.random() - 0.5;});

http://jsfiddle.net/dJVHs/

Easiest method (not perfect shuffle, but in some cases may be better):

function randomize(a, b) {
    return Math.random() - 0.5;
}

yourQuestionArray.sort(randomize);
yourTopicArray.sort(randomize);

or

yourQuestionArray.sort(function (a, b) {return Math.random() - 0.5;});
yourTopicArray.sort(function (a, b) {return Math.random() - 0.5;});

( http://jsfiddle.net/dJVHs/ )

国产ˉ祖宗 2024-09-26 15:55:21

我发现这篇文章关于使用Fisher-Yates 算法 在 JavaScript 中对数组进行打乱。它使用这个函数:

function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

I found this post on using the Fisher-Yates algorithm to shuffle an array in JavaScript. It uses this function:

function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文