0

I have an array object from where I want to select the top 5 value sorting a specific value. below is my code. In the loop, I am getting all the key and value. I just want to pick the top five. Could you please help me with it?

   async function renderCurrency() {
    let currencies = await getCoins();
    //console.log(currencies,'JSHR')
    let html = '';
    var coinlist = [];
    
    coinlist.push(currencies);
    console.log(coinlist);
    //var coinlist = Object.entries(currencies).slice(0, 5);
      
    for (const [key, value] of Object.entries(coinlist)) {      
      for (const [key1, value1] of Object.entries(value)) {
        console.log(key1,value1,"hello")
        

        var diff = value1.last_traded_price-value1.highest_buy_bid;
        var change = diff.toFixed(2);
        let htmlSegment = `<tr>
                              <td><img src="icons/bitcoin.png">${key1}/INR</td>
                              <td>${value1.highest_buy_bid}</td>
                              <td>${change}</td>
                          </tr>`;
      html += htmlSegment;

      }
    }


    let banner = document.querySelector('#tablebody');
    banner.innerHTML = html;

  }

  renderCurrency();

Below is the response I get,

  BTC {highest_buy_bid: 2651378.79, lowest_sell_bid: 2652000, last_traded_price: 2650000, volume: {…}} hello
index.html:77 ETH {highest_buy_bid: 156691.57, lowest_sell_bid: 157470, last_traded_price: 157471.88, volume: {…}} hello
index.html:77 XRP {highest_buy_bid: 48.14, lowest_sell_bid: 48.19, last_traded_price: 48.19, volume: {…}} hello
index.html:77 BNS {highest_buy_bid: 5.45, lowest_sell_bid: 5.49, last_traded_price: 5.45, volume: {…}} hello
index.html:77 DOGE {highest_buy_bid: 16.75, lowest_sell_bid: 16.78, last_traded_price: 16.785, volume: {…}} hello
index.html:77 ADA {highest_buy_bid: 98.51, lowest_sell_bid: 98.92, last_traded_price: 98.93, volume: {…}} hello
index.html:77 DOT {highest_buy_bid: 1275, lowest_sell_bid: 1280.41, last_traded_price: 1280.41, volume: {…}} hello
index.html:77 AAVE {highest_buy_bid: 17593.87, lowest_sell_bid: 17721.21, last_traded_price: 17593.87, volume: {…}} hello
index.html:77 BNB {highest_buy_bid: 22600.06, lowest_sell_bid: 22705.08, last_traded_price: 22705.08, volume: {…}} hello
index.html:77 SOL {highest_buy_bid: 2329.13, lowest_sell_bid: 2348.6, last_traded_price: 2355.7, volume: {…}} hello
index.html:77 MATIC {highest_buy_bid: 93.5, lowest_sell_bid: 93.654, last_traded_price: 93.5, volume: {…}} hello
index.html:77 UNI {highest_buy_bid: 1401.19, lowest_sell_bid: 1414.38, last_traded_price: 1406.14, volume: {…}} hello
index.html:77 CRV {highest_buy_bid: 120.17, lowest_sell_bid: 121.45, last_traded_price: 120.4, volume: {…}} hello
index.html:77 YFI {highest_buy_bid: 2451887.5, lowest_sell_bid: 2475377.95, last_traded_price: 2451887.5, volume: {…}} hello
index.html:77 CAKE {highest_buy_bid: 1039.18, lowest_sell_bid: 1049.52, last_traded_price: 1043.75, volume: {…}} hello

Here I want to choose which has highest_buy_bid. Top 5 highest_buy_bid data I want.

Below is the console. value of coinlist,

[
{
    "BTC": {
        "highest_buy_bid": 2654397.1,
        "lowest_sell_bid": 2661621.46,
        "last_traded_price": 2660713.11,
        "volume": {
            "max": "2667629.75",
            "min": "2275000.00",
            "volume": 126.85254075
        }
    },
    "ETH": {
        "highest_buy_bid": 156772.34,
        "lowest_sell_bid": 157565.36,
        "last_traded_price": 157565.36,
        "volume": {
            "max": "158316.38",
            "min": "134430.00",
            "volume": 1487.425125
        }
    },
    "XRP": {
        "highest_buy_bid": 48.11,
        "lowest_sell_bid": 48.52,
        "last_traded_price": 48.31,
        "volume": {
            "max": "48.79",
            "min": "40.16",
            "volume": 1997933.64
        }
    },
    "BNS": {
        "highest_buy_bid": 5.34,
        "lowest_sell_bid": 5.49,
        "last_traded_price": 5.49,
        "volume": {
            "max": "5.49",
            "min": "5.22",
            "volume": 949341.5946
        }
    },
    "DOGE": {
        "highest_buy_bid": 16.819,
        "lowest_sell_bid": 16.9,
        "last_traded_price": 16.9,
        "volume": {
            "max": "17.259",
            "min": "12.967",
            "volume": 8074960
        }
    },
    "ADA": {
        "highest_buy_bid": 99.62,
        "lowest_sell_bid": 99.87,
        "last_traded_price": 99.64,
        "volume": {
            "max": "99.29",
            "min": "79.00",
            "volume": 2979017.1
        }
    },
    "DOT": {
        "highest_buy_bid": 1275.2,
        "lowest_sell_bid": 1276.99,
        "last_traded_price": 1276.99,
        "volume": {
            "max": "1287.86",
            "min": "1027.33",
            "volume": 39292.832
        }
    },
    "AAVE": {
        "highest_buy_bid": 17713.21,
        "lowest_sell_bid": 17851.88,
        "last_traded_price": 17851.88,
        "volume": {
            "max": "17734.13",
            "min": "13496.90",
            "volume": 990.56
        }
    },
    "BNB": {
        "highest_buy_bid": 22690.76,
        "lowest_sell_bid": 22705.08,
        "last_traded_price": 22705.08,
        "volume": {
            "max": "22705.08",
            "min": "18051.97",
            "volume": 6375.24
        }
    },
    "SOL": {
        "highest_buy_bid": 2345.86,
        "lowest_sell_bid": 2366.3,
        "last_traded_price": 2337.96,
        "volume": {
            "max": "2421.79",
            "min": "1610.00",
            "volume": 21972.6782
        }
    },
    "MATIC": {
        "highest_buy_bid": 94.349,
        "lowest_sell_bid": 94.6,
        "last_traded_price": 94.6,
        "volume": {
            "max": "94.990",
            "min": "73.380",
            "volume": 718651
        }
    },
    "UNI": {
        "highest_buy_bid": 1399.87,
        "lowest_sell_bid": 1413.89,
        "last_traded_price": 1414.38,
        "volume": {
            "max": "1414.69",
            "min": "1093.00",
            "volume": 1237.36
        }
    },
    "CRV": {
        "highest_buy_bid": 120.09,
        "lowest_sell_bid": 121.4,
        "last_traded_price": 120.94,
        "volume": {
            "max": "121.40",
            "min": "96.35",
            "volume": 366580.499
        }
    },
    "YFI": {
        "highest_buy_bid": 2458027.51,
        "lowest_sell_bid": 2481471.92,
        "last_traded_price": 2458482.18,
        "volume": {
            "max": "2493390.21",
            "min": "1971910.23",
            "volume": 7.117055
        }
    },
    "CAKE": {
        "highest_buy_bid": 1039.8,
        "lowest_sell_bid": 1049.71,
        "last_traded_price": 1036.4,
        "volume": {
            "max": "1050.68",
            "min": "757.68",
            "volume": 25846.41
        }
    }
}

]

Thanks

Suman Kalyan
  • 490
  • 1
  • 5
  • 15

1 Answers1

0

Please try like this

async function renderCurrency() {
    let currencies = await getCoins();
    //console.log(currencies,'JSHR')
    let html = '';
    var coinlist = [];
    
    coinlist.push(currencies);
      
    coinlist.map(value=>{
        for(var i in value) {
            value[i].key = i;
        }
        let top5 = Object.values(value).sort(function(a, b){
            return a.highest_buy_bid < b.highest_buy_bid ? 1 : -1
        }).splice(0, 5)
        top5.map(e=>{
            var key = e.key;
            delete e.key;
            console.log(key,e,"hello")
            var diff = e.last_traded_price-e.highest_buy_bid;
            var change = diff.toFixed(2);
            let htmlSegment = `<tr>
                                <td><img src="icons/bitcoin.png">${key}/INR</td>
                                <td>${e.highest_buy_bid}</td>
                                <td>${change}</td>
                            </tr>`;
            html += htmlSegment;
        })
    })


    let banner = document.querySelector('#tablebody');
    banner.innerHTML = html;

}

renderCurrency();
Great Coder
  • 397
  • 3
  • 14
  • This will be return the result what you want. – Great Coder Jun 23 '21 at 07:01
  • I wish we to get the cooperation each other. :D – Great Coder Jun 23 '21 at 07:01
  • still getting the names as undefined. :( undefined "CAKE" "hello" undefined {highest_buy_bid: 2636870.33, lowest_sell_bid: 2650492.45, last_traded_price: 2637026.39, volume: {…}} "hello" undefined {highest_buy_bid: 2442318.02, lowest_sell_bid: 2465391.03, last_traded_price: 2435321.79, volume: {…}} "hello" undefined {highest_buy_bid: 156130.44, lowest_sell_bid: 156757.69, last_traded_price: 156129.51, volume: {…}} "hello" undefined {highest_buy_bid: 22500, lowest_sell_bid: 22503.26, last_traded_price: 22500, volume: {…}} "hello" – Suman Kalyan Jun 23 '21 at 07:03
  • Now, all is fine. I tested the result – Great Coder Jun 23 '21 at 07:12
  • Thanks, mate. it works well now. You can connect with me on Twitter IamSumankalyan – Suman Kalyan Jun 23 '21 at 07:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234102/discussion-between-zeus-of-it-and-suman-kalyan). – Great Coder Jun 23 '21 at 07:21