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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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