Determine the length of the array and come up with a unique line of code

Below is the code snippet I am currently working on:

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.000...
const data = JSON.parse(json);
var Table = require('cli-table');
const chalk = require("chalk"

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

// Converting to array format
const asArray = processed.map(Object.values);

// Displaying the processed data in a tabular form
var table = new Table({
    head: [chalk.green.bold('Coin'), chalk.green.bold('Available'), chalk.green.bold('BTC Value')]
  , colWidths: [25, 25, 25]
});

// Pushing data into the table regardless of the array length
asArray.forEach(item => {
    table.push(item);
});

// Converting the table for display
var tableDisplay = table.toString();

console.log(tableDisplay);

The goal is to automatically include each index of the 'asArray' into the table without manually changing the code every time.

With this updated approach, the code can adapt to the array's length dynamically.

Answer №1

If you want to iterate through your asArray, one way is to use a forEach loop. This loop will traverse each element in the array until it reaches the end.

asArrays.forEach( item => {
    result.push(item)
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for successfully passing arguments to an arrow function in JavaScript

I'm currently working on a react component and I need to call the same arrow function with different arguments. However, I'm struggling with how to pass these arguments and it's making me question whether or not it's even possible. fu ...

Successfully changing the source and tracking of a video through an onclick button, but the video content remains unchanged

I apologize if this post is a duplicate, but I couldn't find the answer in previous threads. I'm attempting to change the video source using an onclick button. However, even after changing the source, the video remains the same. <video width ...

The styles for Material-UI Popover are not taking effect

I have integrated the Popover component from Material-UI into my application: <Popover anchorEl={AnchorElement} anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }} open={props.isVisible} className='popove ...

Tips for including a currency symbol before an input field using Material UI

Trying to add a dollar sign to the left of an input field using InputAdornment but it's not displaying correctly. Check out my code here: https://codesandbox.io/s/material-demo-wnei9?file=/demo.js ...

Storing and securing passwords in Node/Express using hashing and salting techniques

Utilizing the Crypto module within Node's standard library is a necessity. I have established a POST route dedicated to handling a registration form: app.post('/superadmin/add-account', function(req, res) { // Creating shorthand variable ...

How can I manually transclude content within a directive into two separate locations?

When trying to access the result of ng-repeat, I discovered that using the transclude function and manually compiling could help. However, this method does not work in situations with two places and elements containing ng-repeat. Here is how my code is str ...

What is the best way to update the current timestamp dynamically in Vue.js without needing to refresh the page?

I have a Vue application where I am using a method to retrieve the current timestamp. Although it successfully displays the current time and date, it only updates upon page refresh. I would like for it to update dynamically without requiring a page refresh ...

Customizing Cell Templates in Angular UI-Grid Based on Conditions

I am struggling to display a button in my ui-grid only when the field value is not an empty string. I attempted using the ng-if directive, but it is not functioning as expected. Below is the snippet from my grid options: { field: 'ReleaseStatus&apo ...

Is it possible for me to design a unique path without relying on redux?

I am working on a Loginscreen.js component import React, { Component } from 'react'; import Login from './Login'; class Loginscreen extends Component { constructor(props){ super(props); this.state={ username:'&apo ...

jQuery function to alternate between appearing and disappearing captions every 5 seconds

I have a unique project that requires my image captions to both appear and disappear every X seconds. While I have successfully achieved this effect once, I need the captions to continuously "loop". Here is the code I am currently using: <figure> ...

Differences between Cypress and JQuery objects, and the significance of the cy.wrap function

Currently diving into cypress and experimenting with various testing methods. If anyone could lend me a hand with the following inquiries: 1. I've come to understand that most cypress objects utilize jQuery objects for DOM operations, but what sets t ...

Are the jQuery selectors provided by the client safe to use?

Is it secure to let users input jQuery selectors for selection? I am considering taking a selector string submitted by users, which could potentially be malicious, and using it in $('') to target elements on the webpage. ...

Add an array into another array using a for loop, with the first result being duplicated

In this loop, I am facing an issue while trying to insert an array into another array. Here is the code snippet: function convertFormToArray(form){ var temp={}; var question={}; var allQuestions=[]; for (i = 0; i < form.length; i++ ...

Error encountered when attempting to embed a SoundCloud player in Angular 4: Unable to execute 'createPattern' on 'CanvasRenderingContext2D' due to the canvas width being 0

I attempted to integrate the SoundCloud iframe into my Angular 4 component, but encountered the following error message: Failed to execute 'createPattern' on 'CanvasRenderingContext2D': The canvas width is 0. Here is the iframe code ...

There seems to be an issue with the vue-cli when attempting to build in

Within my project files, there is a .env file present -public -src -.env.development.local In the package.json file, it contains: { "name": "my-website", "version": "0.1.0", "scripts": { ...

The requirement in Angular 9 is to provide a NgModule or NgModuleFactory for the bootstrapping process

After upgrading my Angular application from version 6 to version 9, I encountered a strange error when deploying it to my staging server: An error occurred: You must pass in a NgModule or NgModuleFactory to be bootstrapped Despite researching extensive ...

Cross origin requests for AngularJS 1.2 are limited to HTTP protocols only

Is it possible to set up an Angular app for external access? I'm currently using a factory. Just a note, I have my app hosted on localhost but making requests to a server within the same network. angular.module('demoApp.factories', []) ...

Handling Posts and Gets with Jquery/Javascript

Working on a web application that involves generating numerous post and get requests. I am able to monitor all the requests using Developer Tools in Mozilla/Chrome, as well as with Charles (an app). Is it feasible to develop a jQuery or JavaScript functi ...

Retrieve the current time of day based on the user's timezone

Currently, I am working with a firebase cloud function that is responsible for sending push notifications to my app. My main requirement is to send notifications only during the day time. To achieve this, I have integrated moment-timezone library into my p ...

Creating a custom decision tree in Angular/JS/TypeScript: A step-by-step guide

My current project involves designing a user interface that enables users to develop a decision tree through drag-and-drop functionality. I am considering utilizing GoJS, as showcased in this sample: GoJS IVR Tree. However, I am facing challenges in figuri ...