【发布时间】:2021-10-19 04:23:19
【问题描述】:
对网站和 javascript 非常陌生。 在论坛上找了一段时间,但找不到我要找的答案,或者至少无法理解我阅读的主题。
我正在尝试制作一个生成器来根据需要创建上述数组的任意组合,而无需重复组合并且每次迭代仅使用每个数组的一项。我还需要添加一些额外的需求,例如迭代的唯一 id 和用于标记所有属性具有相同值的迭代的额外属性。
这是代码
var accesories = ["pijama" , "urban" , "joker" , "joyboy" , "crypto"];
var hats = accesories;
var tshirts = accesories;
var boots = accesories;
var cards = [];
function randomizeParts() {
model.accesories = accesories[Math.floor(Math.random() * 5)];
model.hats = hats[Math.floor(Math.random() * 5)];
model.tshirts = tshirts[Math.floor(Math.random() * 5)];
model.boots = boots[Math.floor(Math.random() * 5)];
};
function addInsomnio (quantity) {
for (let i = 1 ; i <= quantity ; i++){
model = {
id : 0,
accesories: 0,
hats: 0,
tshirts: 0,
boots: 0}
//adding four digits id
i < 10 ? model.id = '000' + i : i < 100 ? model.id = '00' + i : i < 1000 ? model.id = '0' + i : i <= 10000 ? model.id = i :false;
//randomizing parts
randomizeParts()
//checking if rarity was generated
model.accesories === model.hats && model.accesories === model.tshirts && model.accesories === model.boots ? model.rarity = "original" : false;
//checking its unique
// ????
//Pushing a beautifull brand new and unique card
cards.push(model);
}
};
有没有办法在推送之前将随机 模型 与 cards 中的现有对象进行比较,如果该组合已经存在,则可以根据需要将其随机化多次?
注意:计划使用一次,仅生成一个 10,000 个项目的 json 作为对 photoshop 脚本的支持。
【问题讨论】:
-
这是典型的数组大小 (5) 和数量 (4) 吗?还是真实案例的规模或数量要大得多?
-
这只是一个简化的版本来尝试脚本。我认为大小将接近 30,但数组的数量将始终为 4。
-
您是否只是想以随机顺序获得 5x5x5x5 的排列? (即 5 个数组的 625 个总结果)
-
这可能会派上用场:phrogz.net/lazy-cartesian-product
-
@Jamiec 是的,但不需要所有排列。
标签: javascript arrays sorting random generator