gpt4 book ai didi

javascript - 在React Native中找到数组元素的总和

转载 作者:行者123 更新时间:2023-12-03 12:54:48 27 4
gpt4 key购买 nike

在这里,我正在映射financialTransactionDetail数组并填充数据。值(value)观正在变好。现在,我必须获取TransactionAmount的总和,就像数组的长度为3就有3 TransactionAmount的总和一样,我必须找到1050 + 1050 + 1050 = 3150的总和。我尝试使用load hash,但未获得正确的值。
请帮忙 。

// Below is the array value 
financialTransactionDetail: Array(3)
0:
AdjustedAmount: "0"
NetTransactionAmount: "1050"
TransactionAmount: 1050
1:
AdjustedAmount: "0"
NetTransactionAmount: "1050"
TransactionAmount: 1050

2:
AdjustedAmount: "0"
NetTransactionAmount: "1050"
Status: "Unpaid"
TransactionAmount: 1050

__typename: "FinancialTransactionDetail"
///////////////////////
<Tab heading="INVOICE SPECIFIC">
{ !_.isEmpty(financialTransactionDetail.financialTransactionDetail) && financialTransactionDetail.financialTransactionDetail.map(
(data, index) => {
// this.state.sum+= Sum(parseInt( data.TransactionAmount));
this.state.transactionAmount=_.sum(data.TransactionAmount) ;
console.log("sum data ",this.state.transactionAmount);
return(
<View key={index} style={{flexDirection:'row', padding:10, alignItems:'center', justifyContent:'space-between'}}>
<View style={{paddingRight:10, marginRight:10}}>
<CheckBox style={styles.checkBox} color="#00678f" checked={this.state.isChecked} onPress={() =>this.handleChange()}/>
</View>
<View style={{flexDirection:'column',flex:1, padding:10, borderWidth:1, borderColor:'lightgrey', borderRadius:10}}>
<View style={{flexDirection:'row', alignItems:'center'}}>
{!this.state.isChecked && <RegularText text={`₦ ${data.TransactionAmount}`} style={{paddingBottom:10, paddingRight:5}}/>}
<SmallText text="From 1-Jan-2019 to 31-Jan-2019" style={{paddingBottom:10}}/>
</View>
{this.state.isChecked &&
<RegularText text={`₦ ${data.TransactionAmount}`} style={{borderColor: '#00fff', borderBottomWidth:1}}>
</RegularText>
/* <Input
value={this.state.transactionAmount}
onChangeText={(text) => this.setState({value1:text})}
/> */
}
</View>
</View>

最佳答案

您可以使用纯JavaScript方法reduce对值求和:

let financialTransactionDetail = 
[
{
AdjustedAmount: "0",
NetTransactionAmount: "1050",
TransactionAmount: 1050
},
{
AdjustedAmount: "0",
NetTransactionAmount: "1050",
TransactionAmount: 1050
},
{
AdjustedAmount: "0",
NetTransactionAmount: "1050",
Status: "Unpaid",
TransactionAmount: 1050
}
];

let sum = financialTransactionDetail.reduce((a, c) => { return a + c.TransactionAmount}, 0);
console.log('sum: ', sum)

关于javascript - 在React Native中找到数组元素的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57461304/

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