Struggling with mapping through a multidimensional array?

I'm facing an issue with using .map() on a nested array. I initially tried to iterate through my stored data using .map(), and then attempted another iteration within the first one to handle the nested array, but it didn't work as expected.

The "newmap" seems to be functioning correctly, but it produces a nested array. However, when I try to iterate over "newmap" again, it doesn't lead to any changes, which is where the problem arises.

Below, you'll find all the relevant details:

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import React from 'react'
import { bsctokendata } from "../pages/bscdatanew/data";

export default function Home(props) {
  const databalone = props.addressbalance;
  const datatransone = props.addresstransaction;

  return (
    <ul>      
      <h1>Address One</h1>
      {databalone.map((balance) => {
        return (
          <li>{(balance.result * 1e-18).toString()}</li>
        )
      })}
      <div>
      <h1>Address Two</h1>
      <div>
      {datatransone.map(function(d){
         return (
         <li>{d.result.map((r) => 
         <span>{r.from}</span>)}</li>
         )
       })}
       <h1>Address Three</h1>
        {datatransone.map(function(d){
         return (
         <li>{d.result.map((r) => 
         <span>{r.hash}</span>)}</li>
         )
       })}
      </div>
      </div>
    </ul>
  );
}

export async function getServerSideProps(context) {
    let newmap = bsctokendata.map(function(element){
      return (element.whitelistWallets.map(function(r) { return (r)}))
      // Handling nested arrays
    })

    console.log(newmap)

    let balance = newmap.map(function(element){
      let bscbalance = 'https://api-testnet.bscscan.com/api?module=account&action=tokenbalance&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&tag=latest&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';
      return bscbalance;
    })

    let transaction = newmap.map(function(element){
      let bsctransaction = 'https://api-testnet.bscscan.com/api?module=account&action=tokentx&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&page=1&startblock=0&offset=1&endblock=999999999&sort=asc&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';
      return bsctransaction;
    })

    console.log(transaction)

    const addressbalance = await Promise.all(balance.map(u => fetch(u)))
    const addresstransaction = await Promise.all(transaction.map(e => fetch(e)))

    return {
        props: {
            addressbalance: await Promise.all(addressbalance.map(r => r.json())),
            addresstransaction: await Promise.all(addresstransaction.map(p => p.json())),
        }
    };
}
Check out the result after .map() - having trouble making the first array disappear

Answer №1

start by utilizing the flat() function

const list = [
  [
    "0xb4e856c2a257457b862daed14967578bbdba3526",
    "0x52ab04656c807a3af96f98e4428291e813c2687a"
  ]
]

list.flat().map(item => console.log(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

Retrieving input data when utilizing ng-file-upload

I need help with uploading images and their titles using ng-file-upload in the MEAN stack. I am able to save the image successfully, however, I'm facing difficulty in retrieving the data sent along with it. Controller: module.exports = function ($sc ...

Differences in Loading Gif Visualization Across Chrome, Safari, and Firefox

Having an issue with a loading image in Chrome, while it displays correctly in Safari and Firefox. In Chrome, I see two half-loading images instead of one whole image. Struggling to find a solution for this problem, any assistance would be greatly apprecia ...

Tips for effectively parsing extensive nested JSON structures?

JSON Data on PasteBin I need assistance in converting this JSON data into an Object. It seems valid according to jsonlint, but I'm encountering issues with parsing it. Any help would be greatly appreciated. "Data":[{...},{...},] // structured like t ...

What is the reason for using 'app' as the top-level directory name in React Native import statements within a project setting?

Seeking to comprehend the imports within React Native source code, specifically in a file named questionnaire.actions.js, relative to the top-level directory called lucy-app: ./src/containers/newUserOnboarding/questionnaire/questionnaire.actions.js The m ...

Submitting form data including file uploads using AJAX

Currently, the file is being sent via AJAX using the following code: var fd = new FormData(); //additional actions to include files var xhr = new XMLHttpRequest(); xhr.open('POST', '/Upload/' + ID); xhr.send(fd); In ...

A Guide to Implementing v-for in a JSON Array with Vue.js

After receiving JSON data from the server: [ {"id":"2","name":"Peter","age":"24"}, {"id":"4","name":"Lucy","age":"18"}, ] I am ...

Converting a string into an array with MATLAB

I need help transforming a string input, such as "[f1 f2]", where both f1 and f2 are integers, into an array containing two integers: [f1 f2]. Any suggestions on how to accomplish this task? ...

Angular appends "string:" in front of value when using ng-options

In my HTML, I have a ng-options list set up with a select element like this: <select required="required" ng-model="vm.selectedOption" ng-options="item.name as item.label for item in vm.options"> <option value="">Select an opti ...

Unexpected outcomes arising from using nested arrays with jQuery validation plugin

I have been utilizing the jQuery validation plugin from . I am encountering an issue with validating a nested array "tax_percents[]" that is within another array. The validation seems to only work for the first value in the array, and even for the second f ...

Update dynamically generated CSS automatically

Is there a way to dynamically change the CSS? The problem I'm facing is that the CSS is generated by the framework itself, making it impossible for me to declare or modify it. Here's the scenario at runtime: I am looking to target the swiper-pa ...

Showing a div with 2 unique data attributes - such as displaying Currency and Quantity

My E-Commerce website is created using HTML, JavaScript, and PHP. On the product details page, users can add products to their cart, so I show the total cart value. I need the total amount displayed in a decimal number format (10,2). Currently, when a u ...

Guide on adding increasing numbers to Slug with Mongoose

Currently, I am working on a nodejs project using mongoose. My goal is to ensure that when I save a "slug" into the database, it checks if the slug already exists and adds a counter to it before saving. This means that if I have slugs like my-title, my-tit ...

three.js fur effect not appearing on screen

I have been working on understanding and implementing fur in three.js. I came across an example at which I used as a reference to comprehend the code. The model loads successfully, but the issue arises when the fur texture doesn't load. I have check ...

Performance Comparison: Sorting Arrays vs Vectors in C++ (Vectors are approximately 2.5 times slower than arrays in my specific scenario with no optimizations

Currently, I am implementing an insertion sort on a set of 100000 elements using two different functions. 1- The first function involves copying the vector to be sorted into an array, applying the sort algorithm, and then transferring the sorted array bac ...

Solving Problems with Inline Tables using Angular, Express, and Mongoose's PUT Method

For the past few days, I've been struggling to figure out why my PUT request in my angular/node train schedule application isn't functioning properly. Everything else - GET, POST, DELETE - is working fine, and I can successfully update using Post ...

When selecting an option from jQuery autocomplete, the suggestions data may not always be returned

I have a question about the jQuery autocomplete feature. In my code snippet below, I have an input with the ID aktForm_tiekejas that uses the autocomplete function: $('#aktForm_tiekejas').autocomplete({ serviceUrl: '_tiekejas.php', wid ...

Tips for retrieving the Solana unix_timestamp on the front-end using JavaScript

Solana Rust smart contracts have access to solana_program::clock::Clock::get()?.unix_timestamp which is seconds from epoch (midnight Jan 1st 1970 GMT) but has a significant drift from any real-world time-zone as a result of Solana's processing delays ...

The thumbnail image is failing to load, and when I hover over an image, it moves upwards

I seem to be encountering an issue with a website I uploaded for testing. Everything appears to be working correctly when checked locally in Dreamweaver CS6. However, once the site is uploaded, some issues arise. When hovering over the images, a problem is ...

What is the best way to implement rotation functionality for mobile devices when dragging on a 3D globe using D3.js and an HTML canvas?

I have been experimenting with the techniques demonstrated in Learning D3.js 5 mapping to create a 3D globe and incorporate zoom and rotation functionalities for map navigation. Here is the function that handles both zooming and dragging on devices equipp ...

Can someone explain why my button icons are not aligning to the center properly?

I have a small issue with the icons I pulled from react-icons - they appear slightly raised within the buttons. How can I position them in the middle? That's my only query, but I can't post it alone due to mostly having code in my post. What an ...