Home > Enterprise >  Unable to .map() through nested array?
Unable to .map() through nested array?

Time:04-08

I am not able to .map() through nested array for some reason. I firstly .map() through stored data and then I tried reiterate with another .map() inside my first iteration but it does not resolve the nested array and it stays the same.

The "newmap" works fine but gives me a nested array and when I try to reiterate over "newmap" it does not change anything and that's the problem.

I will show you all relevant information below.

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;
 
  // console.log(datatransone)

  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)}))
      //let wlunarush = element.whitelistWallets;
      //return wlunarush;
      })

      //works fine in the console but does not work using map => problem is nested array

      console.log(newmap)

    // let contractaddress = '0xde301D6a2569aEfcFe271B9d98f318BAee1D30a4';

    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())),
    }
  };


}

export const bsctokendata = [
    {
      tokenName: "Kaiju",
      whitelistWallets: ['0xb4e856c2a257457b862daed14967578bbdba3526', '0x52ab04656c807a3af96f98e4428291e813c2687a'],
      tokenAddress: "0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045"
    }
  ];

Result after .map() / not possible to let disappear the first array

CodePudding user response:

use flat() firstly

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

data.flat().map(i => console.log(i))

  • Related