gpt4 book ai didi

javascript - 自动生成文档的随机测试数据以播种 MongoDB 数据库

转载 作者:行者123 更新时间:2023-12-01 00:58:51 26 4
gpt4 key购买 nike

我正在使用 JSON 生成器 https://next.json-generator.com为我的 MongoDB 数据库提供种子。我发现很难从预定义的组中生成一组随机颜色(从一到六种)。

我已经成功生成了除颜色之外的所有其他属性值。我需要 availableColors 键从预定义的六种颜色中生成随机可变数量的颜色:“蓝色”、“棕色”、“绿色”、“白色”、“黄色”、“灰色”。

这里是在线生成器的 URL,可以对其进行实时编辑:https://next.json-generator.com/E1g60a1pL

这是代码:

[
{
'repeat(5, 10)': {
id: '{{objectId()}}',
name: '{{firstName()}}',
price: '{{floating(5, 4000, 2, "$0,0.00")}}',
availableColors: (colors) => {
var condition = '{{integer(1, 6)}}';
var color = [];
for (j = 0; j < condition+1; j++)
{color [j] = '{{random("blue", "brown", "green", "white", "yellow", "gray")}}';}
let unique_array = [];
for(let i = 0;i < color.length; i++) {
if(unique_array.indexOf(color[i]) == -1){
unique_array.push(color[i]);
}
}
return unique_array;
},
weight: '{{floating(1, 4000, 2, "0.00")}}',
inStock: '{{integer(0, 2000)}}'
}
}
]

这是我得到的结果:

[
{
"id": "5ce82b1302c9777aac5fd681",
"name": "Blake",
"price": "$389.53",
"availableColors": [],
"weight": "3753.22",
"inStock": 449
},
{
"id": "5ce82b137ab9fe24eda22714",
"name": "Felicia",
"price": "$3,190.01",
"availableColors": [],
"weight": "3797.51",
"inStock": 1288
},
{
"id": "5ce82b135414eb7550aee368",
"name": "Bettye",
"price": "$227.41",
"availableColors": [],
"weight": "2182.52",
"inStock": 1288
},
{
"id": "5ce82b13f751e63a8506fbf2",
"name": "Mullen",
"price": "$3,306.81",
"availableColors": [],
"weight": "694.51",
"inStock": 821
},
{
"id": "5ce82b130544c7c08086a6bc",
"name": "Angelita",
"price": "$734.90",
"availableColors": [],
"weight": "3.44",
"inStock": 226
},
{
"id": "5ce82b130d9e2fc4c2a21e22",
"name": "Mcknight",
"price": "$3,582.76",
"availableColors": [],
"weight": "1183.82",
"inStock": 1917
},
{
"id": "5ce82b13fb509ee9c384a096",
"name": "Nannie",
"price": "$3,479.29",
"availableColors": [],
"weight": "754.85",
"inStock": 716
},
{
"id": "5ce82b13881cb29ec7a1772b",
"name": "Sutton",
"price": "$1,726.83",
"availableColors": [],
"weight": "1382.76",
"inStock": 1911
},
{
"id": "5ce82b1386ad13bffcf0923b",
"name": "Maria",
"price": "$1,679.58",
"availableColors": [],
"weight": "1106.28",
"inStock": 5
},
{
"id": "5ce82b13fccd87dbe6451971",
"name": "Noble",
"price": "$309.25",
"availableColors": [],
"weight": "1657.83",
"inStock": 235
}
]

我希望任何文档的“availableColors”都是一到六种预定义颜色的数组。有什么想法吗?

最佳答案

您需要使用 Math.random 创建一个纯 JavaScript 循环我已经更新了链接: https://next.json-generator.com/EJXO4xfaU

关于javascript - 自动生成文档的随机测试数据以播种 MongoDB 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56297481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com