I have an object with 8 items - i want to split those items up into 2 arrays (randomised).
What i want to achieve:
object: {1, 2, 3, 4, 5, 6} : harcoded
From object, it should automatically create 2 separate arrays and take object items and randomise them into the array. Making sure that it does not repeat.
array 1: [3, 5, 6]
array 2: [2, 1, 4]
Code so far:
var element = {
1: {
"name": "One element",
"other": 10
},
2: {
"name": "Two element",
"other": 20
},
3: {
"name": "Three element",
"other": 30
},
4: {
"name": "Four element",
"other": 40
},
5: {
"name": "Five element",
"other": 50
},
6: {
"name": "Six element",
"other": 60
},
7: {
"name": "Seven element",
"other": 70
},
8: {
"name": "Eight element",
"other": 80
}
};
function pickRandomProperty(obj) {
var result;
var count = 0;
for (var prop in obj)
if (Math.random() < 1 / ++count)
result = prop;
return result;
}
console.log(pickRandomProperty(element));