3

Here I am mapping financialTransactionDetail array and populating data. Values are coming 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>

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

@AbhigyanGaurav I am glad to help you! :)
could you please help me for this , after this everything would be done . Please stackoverflow.com/questions/57472798/…
@AbhigyanGaurav man, sorry, but I am not greatly familiar with React.But you can create jsfiddle.net a sample with your sample code and people will try to help you.
2

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.