0

I'm using the following to fetch data from json.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

.

<script>
$.getJSON("https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk", function(data)

{$('#donation_pot_1').text(data.data.received_value);
});

$.getJSON("https://www.chain.so/api/v2/address/DOGE/DLztFgLx33AMMiWFvLkbwy3cDdiuE9ZTrS", function(data) {
$('#donation_pot_2').text(data.data.received_value);
});
</script>

Then displaying the data in divs:

<div id="donation_pot_1"></div>
<div id="donation_pot_2"></div>

I would like to add these two values together (and if possible, remove the numbers after the decimal place) I'm not sure where to start. Any help is greatly appreciated.

1
  • What do you mean by "remove the numbers after the decimal place"? I don't see any decimals. Commented May 10, 2018 at 18:19

2 Answers 2

2

You can do it by chaining the request as said by Jonathan Lam in his answer and you can use parseInt method to remove the decimal values like this

$.getJSON("https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk", function(data1) {
  $.getJSON("https://www.chain.so/api/v2/address/DOGE/DLztFgLx33AMMiWFvLkbwy3cDdiuE9ZTrS", function(data2) {
    var combinedText = parseInt(data1.data.received_value) + parseInt(data2.data.received_value);
    $("#donation_pot_1").text(combinedText);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="donation_pot_1"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

You can chain the two by putting the second JSON request in the callback of the first. This means that the second JSON request will be called after the first (which may be slightly slower), but is an easy way to get the received results in order.

For example:

$.getJSON("https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk", function(data1) {
  $.getJSON("https://www.chain.so/api/v2/address/DOGE/DLztFgLx33AMMiWFvLkbwy3cDdiuE9ZTrS", function(data2) {
        var combinedText = data1.data.received_value + data2.data.received_value;
    });
});

A slightly better approach, if you still want to send both JSON requests at the same time, is to have a callback that makes sure both requests are in before chaining the two together. This again preserves the order of the JSON requests.

This option is also neater, especially with more JSON requests, because you don't have to keep nesting them.

var data = [];
var numJsonRequests = 2;
function callback(index, data) {
    // fill in data
    data[index] = numJsonRequests;

    // check that all requests are in
    for(var i = 0; i < numJsonRequests; i++) {
        if(data[i] === undefined) return;
    }

    // all json requests are in, do something with the combined data
    var combinedData = data.join("");
}

// call callback from json requests
$.getJSON("https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk", function(data) {
  callback(data.data.received_value, 0);
});
$.getJSON("https://www.chain.so/api/v2/address/DOGE/DLztFgLx33AMMiWFvLkbwy3cDdiuE9ZTrS", function(data) {
    callback(data.data.received_value, 1);
});

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.