Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7972227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:48:38+00:00 2026-06-04T07:48:38+00:00

I am trying to only print a unique value of ‘sum’ by doing a

  • 0

I am trying to only print a unique value of ‘sum’ by doing a compare at the end of the loop, but I’m seeing that every time it does a compare it has already moved on to the next element and therefore when its comparing the two values they’re always the same. Is there another way to do this?

 $(document).ready(function(){
    $.getJSON('XML.php', function(data) {
        JSON.stringify(data);
        var prevCardCode = '';

        $.each( data, function(index, element){
            var prevCardCode = element['CardCode'];

            if (!(element['CardCode'] == prevCardCode)) {

                var sum = element['payment_sum'] + '<br/>';
                $('#showdata').append(sum);
                }
                alert(element['CardCode'] + 'compare' + prevCardCode);
        });

    });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:48:39+00:00Added an answer on June 4, 2026 at 7:48 am

    The var keyword doesn’t belong within the each in this case, and you need to move it to after the comparison.

    var prevCardCode = '';
    $.each(data, function(index, element) {
        if (!(element['CardCode'] == prevCardCode)) {
            var sum = element['payment_sum'] + '<br/>';
            $('#showdata').append(sum);
        }
        alert(element['CardCode'] + 'compare' + prevCardCode);
        prevCardCode = element['CardCode'];
    });​
    

    If the var is left in place before prevCardCode, it will never set the one defined outside of the $.each and will not carry over to the next iteration of $.each.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to compare and print only members with Inactive status. The problem is that
I'm trying to print the output of function only when it is true but
I've been trying to print out Google Calendars but they only print for letter
Im trying to print only lines that do not start with a letter from
This may sound like a stupid question, perhaps it is. But I'm only trying
I’ve only been trying it in Firefox’s JavaScript console, but neither of the following
I'm trying to match only numbers and spaces in my php regex but the
I am trying to show only elements that have the title attribute passed to
I'm trying to go through a file, and only print out words without a
i'm trying to read contacts with this code but it only gets Contacts but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.