我試圖在下面的 FlatList 中獲取從我的提取中檢索到的所有值的總和是我的代碼。
<SafeAreaView style={styles.container}>
<FlatList
data={numList}
listKey="number-list"
keyExtractor={item => item.id}
renderItem={({ item }) => {
return (
<View style={styles.ticSec}>
<Text>{item.amount}</Text>
</View>
)
}}
/>
</SafeAreaView>
輸出結果:
10
20
15
我怎樣才能使它的輸出總和總和45是總和10,20,15
謝謝你的幫助
uj5u.com熱心網友回復:
用戶計算您喜歡Array.prototype.reduce的所有專案的金額總和:numList
const sumTotal = numList.reduce((acc, next) => {
return acc next.amount
}, 0)
whereacc代表accumulator并且next是我們迭代的下一個專案。
然后,您可以在 jsx 中使用該值:
<Text>{sumTotal}</Text>
更多關于reduce:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521110.html
