0

I initially used the information from prior questions to figure out how to randomize data using information I found in a Stack Overflow Q&A. (How to randomize (shuffle) a JavaScript array?) In my original attempt, I did separate randomizations for girls' names and boys' names.

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

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

Randomization was completed using the Fisher-Yates algorithm submitted by @gnarf :

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;
}

As I've thought about it and read more, I realize that I really need to use a JSON instead of a simple array. JSON is new to me. I've figured out how to set it up:

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"
}];

I haven't figured out how to call the function correctly or how to do separate randomizations for girls and boys. So, for example, the end of the randomization should replace girls' names with other girls' names but keep npcRisk in the same order. Guidance would be appreciated.

Community
  • 1
  • 1
  • 4
    1) There's no JSON in your question, 2) You've an array, not an object, 3) A duplicate of http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – Teemu Feb 24 '16 at 23:42
  • You have provided an array of JSON objects, but no actual code that demonstrates what you have attempted to parse it. There are quite literally dozens of tutorials on how to work with and parse JSON objects. Here is an oldish, but still completely relevant article that will get you started. Spend time on learning the basics of working with JSON objects, and I will be willing to bet that by the end of that article you will have figured out the answer to your question. http://www.copterlabs.com/json-what-it-is-how-it-works-how-to-use-it/ – Korgrue Feb 24 '16 at 23:47

3 Answers3

0

I'm assuming you want the npcRisk to stay constant and essentially shuffle only names in your JSON objects. Here is a simple solution. It takes name and splits them into two arrays by gender, applies your logic to them then reinserts the new values.

function shuffleJSON(input) {
    var boys = [],
        girls = [];
    for (var i = 0; i < input.length; i++) {
        if (input[i].npcSex === 'boy') {
            boys.push(input[i].npcName);
        }
        if (input[i].npcSex === 'girl') {
            girls.push(input[i].npcName);
        }
    }

    boys = shuffle(boys);
    girls = shuffle(girls);

    for (var i = 0; i < input.length; i++) {
        if (input[i].npcSex === 'boy') {
            input[i].npcName = boys.shift();
        }
        if (input[i].npcSex === 'girl') {
            input[i].npcName = girls.shift();
        }
    }
    return input;
}
0

Why not use the RNG in creating your NPC array?

Off the top of my head...

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
    });
};
John
  • 76
  • 1
  • 6
0

You could try using 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");
guest271314
  • 1
  • 15
  • 104
  • 177