skip to Main Content

I have below two objects and i need to compare values object with points object based on the given answer like ‘Q1A1’ or ‘Q2A1’ and once key match in both object then it should return the sum of their respective answers.

const values = {
    Q1: {
        Q1A1: "Yes",
    },
    Q2: {
        Q2A1: "Yes",
    },
    Q3: {
        Q3A2: "No",
    },
};

const points = {
    Q1A1: 41,
    Q1A2: 0,
    Q2A1: 19,
    Q2A2: 0,
    Q3A1: 25,
    Q3A2: 0,
};

After comparing both above object based on the given answer the sum will be 60. So, how can i return the sum 60 by comparing these object ?

4

Answers


  1. You need to loop your answers. I would suggest using true/false instead of Yes/No:

    const values = {
      Q1: {
        Q1A1: 'Yes'
      },
      Q2: {
        Q2A1: 'Yes'
      },
      Q3: {
        Q3A2: 'No'
      }
    };
    
    const points = {
      "Q1A1": 41,
      "Q1A2": 0,
      "Q2A1": 19,
      "Q2A2": 0,
      "Q3A1": 25,
      "Q3A2": 0
    };
    
    let total = 0;
    
    // First loop your questions
    for (const question in values) {
      // Then loop all answers in that question
      for (const [answer, value] of Object.entries(values[question])) {
        // Check if answer is 'Yes' and if that question even exists in possible question pool
        if (value == 'Yes' && points[answer] != null) {
            // Sum up total points
            total += points[answer];
        }
      }
    }
    
    alert('Your score is ' + total);
    Login or Signup to reply.
  2. This can be done in multiple ways. Here is one :

    const val = Object.values(values)
      .reduce((sum, answers) => {
        sum += Object.entries(answers)
          .reduce((subSum, [answer, value]) => {
            if(value === 'No') return subSum;
            return subSum + points[answer]
           }, 0);
        return sum;
      }, 0);
    
    console.log(val);
    <script>
      const values = {
        Q1: {
          Q1A1: "Yes",
        },
        Q2: {
          Q2A1: "Yes",
        },
        Q3: {
          Q3A2: "No",
        },
      };
    
      const points = {
        Q1A1: 41,
        Q1A2: 0,
        Q2A1: 19,
        Q2A2: 0,
        Q3A1: 25,
        Q3A2: 0,
      };
    </script>
    Login or Signup to reply.
  3. Use the values to drive the script:

    const total = Object.values(values)
      .reduce((acc, cur) => acc + Object.entries(cur)
        .reduce((accInner, [key, val]) => val !== 'No' && points[key] ? accInner + points[key] : accInner
        , 0)
      , 0);
    
    console.log(total)
    <script>
      const values = {
        Q1: {
          Q1A1: "Yes",
        },
        Q2: {
          Q2A1: "Yes",
        },
        Q3: {
          Q3A2: "No",
        },
      };
    
      const points = {
        Q1A1: 41,
        Q1A2: 0,
        Q2A1: 19,
        Q2A2: 0,
        Q3A1: 25,
        Q3A2: 5, // should not be counted
      };
    </script>

    Or (easier to read) make a lookup table and filter+reduce

    const lookup = Object.values(values).reduce((acc, cur) => {
      const [key, val] = Object.entries(cur)[0]
      if (val === "Yes") acc[key] = val;
      return acc;
    }, {})
    
    console.log(lookup)
    
    const val = Object.entries(points)
      .filter(entry => lookup[entry[0]] && entry[1])
      .reduce((a, b) => a[1] + b[1]);
    console.log(val)
    <script>
      const values = {
        Q1: {
          Q1A1: "Yes",
        },
        Q2: {
          Q2A1: "Yes",
        },
        Q3: {
          Q3A2: "No",
        },
      };
    
      const points = {
        Q1A1: 41,
        Q1A2: 0,
        Q2A1: 19,
        Q2A2: 0,
        Q3A1: 25,
        Q3A2: 5, // should not be counted
      };
    </script>
    Login or Signup to reply.
  4. you can try this:

     arr_points.includes(Object.keys(value).join('')) && Object.values(value).join('') === 'Yes'
    
                        ?accumulator +  points[Object.keys(value).join('')]
        
                        :accumulator
    const values = {
        Q1: {
            Q1A1: 'Yes'
        },
        Q2: {
            Q2A1: 'Yes'
        },
        Q3: {
            Q3A2: 'No'
        },
        Q4:{ QE:'No'}
    }
    const points = {
        Q1A1: 41,
        Q1A2: 0,
        Q2A1: 19,
        Q2A2: 0,
        Q3A1: 25,
        Q3A2: 0
    }
    const arr_points = Object.keys(points)
    //
    const Sum  = Object.entries(values).reduce((accumulator,[key, value]) => 
                        arr_points.includes(Object.keys(value).join('')) && Object.values(value).join('') === 'Yes'
    
                        ?accumulator +  points[Object.keys(value).join('')]
        
                        :accumulator
            
    , 0 )
    
    console.log(Sum)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search