Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6211

How can I iterate through a props and increment the values inside the props?

$
0
0

I am trying to increment the values of the props whenever an option is clicked depending on which one I click on a mobile application. Each click takes me to the next page and each page has a list of values for example:

question: 'Who won?',
answers:[
            {
                answerId: 1,
                answerText: 'Blue Team',
                scores:[
                    {
                        john: 0,
                        mary: 1,
                        james: 1,
                        sarah: 0,
                    }
                ],
            },
            {
                answerId: 2,
                answerText: 'Red Team',
                scores:[
                    {
                        john: 1,
                        mary: 0,
                        james: 0,
                        sarah: 1,
                    }
            ],
}

If someone would click on Blue Team we would then add the scores for that answer and make the array keeping the scores have the following values:

{
john: 0,
mary: 1,
james: 1,
sarah: 0,
}

This is a snippet of the code that I am using for this

THE FUNCTION TO ADD PROPS


AddAnswerScoresFunction=(props)=>{
    console.log(props);
    // Sum props every time

    sum = props.reduce((a, b)=>{
      for(let k in props.reduce(b)){
        console.log("k is ", k);
        if(b.hasOwnProperty(k)){
           a[k] =(a[k] || 0) + b[k];


          console.log(a[k]);

        }
      }
    },{});

THE BUTTON ON WHICH WE CALL THE FUNCTION

<Text key={i} style={styles.answer} onPress={() => {this.AddAnswerScoresFunction(unique.scores); Vibration.vibrate(500); this.swiper.swipeLeft()}}>{unique.answerText}</Text>



Viewing all articles
Browse latest Browse all 6211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>