0

Here is my code:

const json = `{"BTC":{"available":0.00024868,"onOrder":0,"btcValue":0.00024868,"btcTotal":0.00024868},"LTC":{"available":0,"onOrder":0,"btcValue":0,"btcTotal":0},"ETH":{"available":0,"onOrder":0,"btcValue":0,"btcTotal":0},"NEO":{"available":0,"onOrder":0,"btcValue":0,"btcTotal":0},"BNB":{"available":0.08943066,"onOrder":0,"btcValue":0.0004663808919,"btcTotal":0.0004663808919}}`;
const data = JSON.parse(json);
var Table = require('cli-table');
const chalk = require("chalk");

const processed = Object.entries(data)
  .filter(([, { available }]) => available > 0)
  .map(([asset, { available, btcValue }]) => {
    return { asset, available, btcValue };
  });

const asArray = processed.map(Object.values);

//console.table(processed);
console.log(asArray);
console.log(asArray.length)


var table = new Table({
    head: [chalk.green.bold('Coin'), chalk.green.bold('Available'), chalk.green.bold('BTC Value')]
  , colWidths: [25, 25, 25]
});

table.push(
    asArray[0], asArray[1]
);

var tableDisplay = table.toString();

console.log(tableDisplay);

I am trying to find a way of finding the asArray length, then putting each index in the table.push no matter how long it is,

So it would list asArray[0], and increase for each index of the array no matter what

the code currently works, but I have to know the length of the array in order to change the code each time.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Rexy
  • 15
  • 5
  • 2
    If the `Table#push()` method supports multiple arguments (like `Array#push()`) you should just use `table.push(...asArray)` to pass all array elements as arguments. – VLAZ Mar 12 '21 at 07:15
  • [Passing an array as a function parameter in JavaScript](https://stackoverflow.com/q/2856059) – VLAZ Mar 12 '21 at 07:20

1 Answers1

0

you can set up a loop to loop through your asArray. A forEach loop will go through each of your element in the array until it reaches the end.

asArrays.forEach( element => {
    table.push(element)
})
Isaac Yong
  • 109
  • 1
  • 5