无法通过嵌套数组进行 .map()?
Unable to .map() through nested array?
出于某种原因,我无法通过嵌套数组执行 .map()。我首先 .map() 通过存储的数据然后我尝试在我的第一次迭代中用另一个 .map() 重复但它没有解析嵌套数组并且它保持不变。
“newmap”工作正常,但给了我一个嵌套数组,当我尝试重复“newmap”时,它没有改变任何东西,这就是问题所在。
我将在下面向您展示所有相关信息。
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
先用flat()
const data = [
[
"0xb4e856c2a257457b862daed14967578bbdba3526",
"0x52ab04656c807a3af96f98e4428291e813c2687a"
]
]
data.flat().map(i => console.log(i))
出于某种原因,我无法通过嵌套数组执行 .map()。我首先 .map() 通过存储的数据然后我尝试在我的第一次迭代中用另一个 .map() 重复但它没有解析嵌套数组并且它保持不变。
“newmap”工作正常,但给了我一个嵌套数组,当我尝试重复“newmap”时,它没有改变任何东西,这就是问题所在。
我将在下面向您展示所有相关信息。
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
先用flat()
const data = [
[
"0xb4e856c2a257457b862daed14967578bbdba3526",
"0x52ab04656c807a3af96f98e4428291e813c2687a"
]
]
data.flat().map(i => console.log(i))