使用JavaScript在对象中随机化(混洗)数组

时间:2016-02-24 23:40:04

标签: javascript random shuffle

我最初使用先前问题的信息来弄清楚如何使用我在Stack Overflow Q& A中找到的信息来随机化数据。 (How to randomize (shuffle) a JavaScript array?)在我最初的尝试中,我为女孩做了单独的随机化。姓名和男孩'名。

var girls = shuffle([
    "Amanda",
    "Deja", 
    "Grace", 
    "Hailey",
    "Jada", 
    "Kylie",
    "Maria",
    "Shanice",
    "Victoria"
]);

var boys = shuffle([
    "Aiden",
    "Benjamin", 
    "Daniel",
    "Isaiah",
    "Jamal",
    "Maurice",
    "Steven",
    "Tyrone",
    "Zach"
]);

使用@gnarf提交的Fisher-Yates算法完成随机化:

function shuffle(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}

正如我已经考虑过并阅读更多内容,我意识到我确实需要使用JSON而不是简单的数组。 JSON对我来说是新的。我已经弄明白了如何设置它:

var npc = [{
  "npcName": "Amanda",
  "npcSex": "girl",
  "npcRisk": "1"
}, {
  "npcName": "Deja",
  "npcSex": "girl",
  "npcRisk": "2"
}, {
  "npcName": "Grace",
  "npcSex": "girl",
  "npcRisk": "3"
}, {
  "npcName": "Hailey",
  "npcSex": "girl",
  "npcRisk": "4"
}, {
  "npcName": "Jada",
  "npcSex": "girl",
  "npcRisk": "5"
}, {
  "npcName": "Kylie",
  "npcSex": "girl",
  "npcRisk": "6"
}, {
  "npcName": "Maria",
  "npcSex": "girl",
  "npcRisk": "7"
}, {
  "npcName": "Shanice",
  "npcSex": "girl",
  "npcRisk": "8"
}, {
  "npcName": "Victoria",
  "npcSex": "girl",
  "npcRisk": "9"
}, {
  "npcName": "Aiden",
  "npcSex": "boy",
  "npcRisk": "1"
}, {
  "npcName": "Benjamin",
  "npcSex": "boy",
  "npcRisk": "2"
}, {
  "npcName": "Daniel",
  "npcSex": "boy",
  "npcRisk": "3"
}, {
  "npcName": "Isaiah",
  "npcSex": "boy",
  "npcRisk": "4"
}, {
  "npcName": "Jamal",
  "npcSex": "boy",
  "npcRisk": "5"
}, {
  "npcName": "Maurice",
  "npcSex": "boy",
  "npcRisk": "6"
}, {
  "npcName": "Steven",
  "npcSex": "boy",
  "npcRisk": "7"
}, {
  "npcName": "Tyrone",
  "npcSex": "boy",
  "npcRisk": "8"
}, {
  "npcName": "Zach",
  "npcSex": "boy",
  "npcRisk": "9"
}];

我还没弄明白如何正确地调用该功能或​​如何为女孩和男孩单独进行随机化。因此,例如,随机化的结束应该取代女孩'与其他女孩的名字'名称但保持npcRisk的顺序相同。指导将不胜感激。

3 个答案:

答案 0 :(得分:0)

我假设您希望npcRisk保持不变,并且基本上只对您的JSON对象中的名称进行洗牌。这是一个简单的解决方案。它需要名称并按性别将它们分成两个数组,将逻辑应用于它们然后重新插入新值。

答案 1 :(得分:0)

为什么不在创建NPC阵列时使用RNG?

脱离我的头顶......

var npcArray = [];
//Set up name arrays
var boysNames = ['Matthew', 'Mark', 'Luke' ....];
var girlsNames = ['Daisy', 'Helen', 'Beth' ....];
for (var i = 1; i <= 9; i++){
    var nameIndex = Math.floor(Math.random() * (boysNames.length - 1));
    //Create the NPC object using inline notation and add it to the array end of npcArray.  N.B. Array.splice() removes an element from the boysName array, hence the use of boysNames.length in the RNG.
    npcArray.push({
        npcName: boysNames.splice(nameIndex, 1),
        npcSex: 'boy',
        npcRisk: i
    });
};
for (var i = 1; i <= 9; i++){
    var nameIndex = Math.floor(Math.random() * (girlsNames.length - 1));
    npcArray.push({
        npcName: girlsNames.splice(nameIndex, 1),
        npcSex: 'girl',
        npcRisk: i
    });
};

答案 2 :(得分:0)

您可以尝试使用Array.prototype.filter()Array.prototype.forEach()setTimeout()

function shuffleProps(arr, prop, term) {
  var obj = {res:[]}; 

  obj[prop] = arr.filter(function(val, key) {
      return val[term] === prop
  });

  obj[prop].forEach(function(val, key, arr) {
    setTimeout(function() {
      obj["res"].push(val)
    }, Math.random() * 10)
  });

  return obj["res"]
}

var boys = shuffleProps(npc, "boy", "npcSex");