我想在遍歷陣列時根據索引從索引 1 以 125 為增量生成一個數字。如果索引為 0,則應列印 100。如果索引為 1,則 100 125 = 225,索引為 2,225 125 = 350 依此類推。我該怎么做呢?這就是我所擁有的。
const rows = 5;
const rowArray = new Array(rows).fill(' ');
const result = rowArray.map((i, index) => {
if (index === 0) {
return 100
}
if (index === 1) {
return 225
}
return index * 100 125
})
console.log(result)
預期輸出:
[100, 225, 350, 475, 600, 725]
有人可以幫我解決這個數學問題嗎?謝謝。
uj5u.com熱心網友回復:
const rows = 5;
const rowArray = new Array(rows).fill(' ');
const result = rowArray.map((i, index) => {
return 100 125*index
})
console.log(result)
這應該會更好:您乘以 100,而不是 125。請注意,index===1并且index===0仍然包含在100 125*index
uj5u.com熱心網友回復:
該result函式可以重寫為:
const result = rowArray.map((i, index) => index * 125 100)
uj5u.com熱心網友回復:
順便說一句,Array#reduce非常適合這個:
const rows = 5;
const rowArray = new Array(rows).fill(' ');
const result = [100];
rowArray.reduce((a, b) => (b = a 125, result.push(b), b), 100);
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461598.html
標籤:javascript
上一篇:如何在javascript中從本地存盤onClick獲取資料
下一篇:使用多種型別的打字稿問題
