1

I want to update the "Last" Price of MarketName USDT-BTC, how do I modify "Last" from 16750.00000001 to 17000.00000001 and send it over my api?

{
  "success":true,
  "message":"",
  "result":[{
    "MarketName":"USDT-BTC",
    "High":16937,
    "Low":15280,
    "Volume":6268.37139646,
    "Last":16750.00000001,
    "BaseVolume":101115016.3188782,
    "TimeStamp":"2017-12-15T01:11:19.513",
    "Bid":16749.99999999,
    "Ask":16750,
    "OpenBuyOrders":12099,
    "OpenSellOrders":4901,
    "PrevDay":16143.70987342,
    "Created":"2015-12-11T06:31:40.633"
}]}
var express = require("express");
var app = express();
const request = require('request');

const options = {
    url: 'https://bittrex.com/api/v1.1/public/getmarketsummaries',
    method: 'GET',
};

app.get("/api", function(req, res)  {
        request(options, function(err, output, body) {
        var json = JSON.parse(body);
        delete json['USDT-BTC']; // THIS IS NOT WORKING WHAT DO I HAVE TO DO?
        console.log(json);
        res.json(json)
});

});

app.listen(80, function() {
    console.log("RUNNING: http://localhost/api");
});

module.exports = app;
1
  • 'USDT-BTC' is a value not a property and it is in an array nested inside another property. Use Array#find() to get that object from the array Commented Dec 15, 2017 at 1:38

2 Answers 2

3

You need to look for the USDT-BTC and change the price:

 for (var i in json.result) {
     var item = json.result[i];

     if (item.MarketName == 'USDT-BTC') {
         item.Price = 17000.00000001;
     }
 }

Other way using map:

 json.result = json.result.map(item => (
     item.MarketName == 'USDT-BTC' ?
         { ...item, price: 17000.00000001} : item );
 )
Sign up to request clarification or add additional context in comments.

1 Comment

The option with map is really clean. Thanks for sharing it.
0

Not sure I get what you are doing. Why are you deleting something? Fist the object key seems to be "Last" not "USDT-BTC". So you would need to get the Jon object find the correct entry and exit the 'Last' key. Something like var entry = json[0]; entry ['Last'] = "new value":

That is assuming you only have 1 thing in the response and/or it is the first element in the returned array.

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.