我正在為我的應用程式進行集成測驗。我想看看我的資料庫可以如何撰寫 cartItems 有效負載。為此,我生成了 cartItems 陣列。在該陣列中,我有一個名為 ean 的專案。我想生成 12 位亂數來 ean。但它總是從陣列中回傳相同的數字。
我面臨兩個問題
- 無法生成 12 位 ean 數字
- 當我生成陣列時,6 位數的 ean 數字總是相同的。但我想要亂數
這是我的代碼
const requestParameters = {
id: "530d275e-5de1-466d-86fe-3993a2563fb6",
cartItems: new Array(500).fill({
additionalInfo: '',
brand: '',
replace: false,
basicQuantityUnit: 'KPL',
collectingPriority: 1000,
ean: Math.floor(100000 Math.random() * 900000) 10000, // I want to render random 12 digits
id: '0200097823340',
itemCount: '1'
})
}
console.log(requestParameters)
uj5u.com熱心網友回復:
您插入了相同的專案500時間,但您希望每次都創建一個新專案。用于map執行此操作
const requestParameters = {
id: "530d275e-5de1-466d-86fe-3993a2563fb6",
cartItems: new Array(5).fill().map(() => ({
additionalInfo: '',
brand: '',
replace: false,
basicQuantityUnit: 'KPL',
collectingPriority: 1000,
ean: Array(12).fill().map(() => Math.floor(Math.random() * 10)).join(''),
id: '0200097823340',
itemCount: '1'
}))
}
console.log(requestParameters)
要生成 12 位數字,我會使用:
Array(12).fill().map(() => Math.floor(Math.random() * 10)).join('')
如果您希望將其作為數字而不是字串,請使用:
Array(12).fill().map(() => Math.floor(Math.random() * 10)).join('')
uj5u.com熱心網友回復:
Math.floor(100000000000 Math.random() * 900000000000)
這會生成 12 位亂數并確保第一個數字不是 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529497.html
標籤:javascript
