3

I am taking an array of objects from my graphql response and writing that to a HTML list.

.then(function(body) {
 
 var x = body.data.ethereum.address[0].balances.map(x => x.currency.address)

    console.log(x)
    //alert(JSON.stringify(y));
   

function makeUL(){
    var a = '<ul>',
        b = '</ul>',
        m = [];


    for (i = 0; i < x.length; i += 1){
        m[i] = '<li>' + x[i] + '</li>';
    }

    document.getElementById('container').innerHTML = a + m + b;
}
    
    makeUL();

 });

This returns a HTML list as follows:

0x2a9718deff471f3bb91fa0eceab14154f150a385
,
0xad90c05bc51672eedfee36e58b3ff1a78bbc146d
,
0x33a3d962955a3862c8093d1273344719f03ca17c
,
0x85102c0062aa918cb9e26d94a284aafca602df13
,
0x07af67b392b7a202fad8e0fbc64c34f33102165b
,
0x31045e7023e6c388f9447e632a3f9eaff90393fa
,
-
,
0x2222227e22102fe3322098e4cbfe18cfebd57c95
,
0xdf6d9fc61bd8163b59d6381ec93ae1d3a4d95bb2
,
0x2e291e1c9f85a86d0c58ae15621aac005a8b2ead
,
0x6b51231c43b1604815313801db5e9e614914d6e4
,
0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3
,
0xdbaaa36b347d56b77ce0e36f050fceebbf9fbc38
,
0x3ad9594151886ce8538c1ff615efa2385a8c3a88
,
0xf7844cb890f4c339c497aeab599abdc3c874b67a
,
0xa57ac35ce91ee92caefaa8dc04140c8e232c2e50

screenshot of HTML list

Is there a way I can remove the commas "," from the list so that my HTML just shows the addresses ?

I've tried using x.join("%") and x.replace(/,/g, "") but by doing that it changes the array to one long string was then isn't suitable for the makeUL function

Gangrel
  • 449
  • 4
  • 20
  • `a + m + b`, converts the `m` array to string which is same as `m.join(",")`. That adds a comma between the `li`. You could change `m = []` to `m = ''` and use `m += '
  • ' + x[i] + '
  • '` in the loop – adiga May 01 '21 at 10:34