如何洗牌JSON.parse数据?

huangapple go评论69阅读模式
英文:

How to shuffle JSON.parse data?

问题

可能将从ajax解析的JSON结果随机排序吗?

$.ajax({
    method: 'GET',
    url: '/-pathSomething-/--ExampleOnly--',
    data: { sample: sample, sample: sample, sample: sample },
    success: function (result) {
        var ExamDisplayQNChoiceML = JSON.parse(result);
    }
});

它看起来像这样 如何洗牌JSON.parse数据?

我不知道如何做到这一点,所以我没有太多的代码要展示...

期望的输出:
随机排序

{ QuesNo: 3, QuesID: 3, ... }
{ QuesNo: 1, QuesID: 1, ... }
{ QuesNo: 4, QuesID: 4, ... }
{ QuesNo: 2, QuesID: 2, ... }
{ QuesNo: 5, QuesID: 5, ... }
英文:

Is it possible to shuffle a json parsed result from ajax?

$.ajax({
    method: 'GET',
    url: '/-pathSomething-/--ExampleOnly--',
    data: { sample: sample, sample: sample, sample: sample },
    success: function (result) {
        var ExamDislayQNChoiceML = JSON.parse(result);
    }
});

it is looks like this 如何洗牌JSON.parse数据?

i don't know how to do it so i don't have much code to show..

expected output.
randomly

{ QuesNo: 3, QuesID: 3, ... }
{ QuesNo: 1, QuesID: 1, ... }
{ QuesNo: 4, QuesID: 4, ... }
{ QuesNo: 2, QuesID: 2, ... }
{ QuesNo: 5, QuesID: 5, ... }

答案1

得分: 1

这可以满足您的要求

let myJSON = {
  "name": "John",
  "age": 30,
  "city": "New York"
};

let myArray = Object.entries(myJSON);

for (let i = myArray.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [myArray[i], myArray[j]] = [myArray[j], myArray[i]];
}

myJSON = Object.fromEntries(myArray);

console.log(myJSON);

这将是输出结果

{
  "city": "New York",
  "name": "John",
  "age": 30
}
英文:

this could do your request

let myJSON = {
  "name": "John",
  "age": 30,
  "city": "New York"
};

let myArray = Object.entries(myJSON);

for (let i = myArray.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [myArray[i], myArray[j]] = [myArray[j], myArray[i]];
}

myJSON = Object.fromEntries(myArray);

console.log(myJSON);

This will be the output

{
  "city": "New York",
  "name": "John",
  "age": 30
}

答案2

得分: 1

使用 Fisher-Yates 洗牌算法,这里是文档链接:https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle,以下是如何使用它来洗牌对象数组的示例:

$.ajax({
    method: 'GET',
    url: '/-pathSomething-/--ExampleOnly--',
    data: { sample: sample, sample: sample, sample: sample },
    success: function (result) {
        var ExamDislayQNChoiceML = JSON.parse(result);
        shuffleArray(ExamDislayQNChoiceML);
        console.log(ExamDislayQNChoiceML); // 洗牌后的数组
    }
});

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
}
英文:

use the Fisher-Yates shuffle algorithm here is a documentation link: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle and Here's an example of how use it to shuffle the array of objects

$.ajax({
    method: 'GET',
    url: '/-pathSomething-/--ExampleOnly--',
    data: { sample: sample, sample: sample, sample: sample },
    success: function (result) {
        var ExamDislayQNChoiceML = JSON.parse(result);
        shuffleArray(ExamDislayQNChoiceML);
        console.log(ExamDislayQNChoiceML); // shuffled array
    }
});

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
}

答案3

得分: 1

你可以将 Math.random() 传递给 Array.sort() 方法来打乱问题对象的数组。Math.random() 返回介于 0 和 1 之间的浮点数。当你从 Math.random() 的结果中减去 0.5 时,Array.sort() 的比较函数会返回随机的正数或负数值,因此数组会被打乱:

const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
const shuffledArr = shuffleArray(arr);
英文:

Assuming that you wanted to shuffle array of questions objects: You can pass Math.random() to Array.sort() method. Math.random() returns float between 0 and 1. When you subtract 0.5 from result of Math.random(), the Array.sort() compare function returns random positive or negative value, so the array is shuffled:

const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
const shuffledArr = shuffleArray(arr);

huangapple
  • 本文由 发表于 2023年2月16日 15:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定