admin管理员组文章数量:1422504
Here I am mapping financialTransactionDetail
array and populating data. Values are ing fine. Now I have to get the sum of TransactionAmount
, like the length of array is 3 there are 3 TransactionAmount
, I have to find the sum of 1050+1050+1050 = 3150. I tried suing load hash , but not getting proper value .
Please help .
// 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>
Here I am mapping financialTransactionDetail
array and populating data. Values are ing fine. Now I have to get the sum of TransactionAmount
, like the length of array is 3 there are 3 TransactionAmount
, I have to find the sum of 1050+1050+1050 = 3150. I tried suing load hash , but not getting proper value .
Please help .
// 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>
Share
Improve this question
edited Aug 12, 2019 at 13:18
Lajos Arpad
77.6k41 gold badges117 silver badges224 bronze badges
asked Aug 12, 2019 at 12:33
Abhigyan GauravAbhigyan Gaurav
1,9047 gold badges29 silver badges63 bronze badges
2 Answers
Reset to default 6You can sum your values using pure JavaScript method 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)
You can simply iterate through your array and sum the values:
for (var sum = 0, index = 0; index < financialTransactionDetail.length; index++) sum += financialTransactionDetail[index].TransactionAmount;
本文标签: javascriptFind the sum of array element in react nativeStack Overflow
版权声明:本文标题:javascript - Find the sum of array element in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745371389a2655747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论