與這個有一段艱難的時光。我一直在尋找,但似乎沒有找到類似的場景......
我正在嘗試查找用戶擁有的每個集合的作者。
我已經制作了這個函式來從 api 中提取資料,在這種情況下,一個特定的用戶是質子,如鏈接末尾所示:
function BCAd() {
const [bcOwner, setBcOwner] = useState<any>();
useEffect(() => {
async function fetchData() {
const res = await fetch(
'https://proton.api.atomicassets.io/atomicassets/v1/accounts/proton'
);
const { data } = await res.json();
setBcOwner(data);
}
fetchData();
}, []);
if (!bcOwner) {
return (
<div>
<Spinner />
</div>
);
}
console.log(bcOwner);
return null;
}
export default BCAd;
問題是,當我 consol.log bcOwner時,作者在一個陣列(集合)內,該陣列(集合)在另一個陣列(0)內,它在一個物件(集合)內......所以我需要進入集合和然后在每個0內,然后是收集以獲取作者(這句話不是必需的,但我輸入它以確保我得到我正在做的事情,哈哈)。我需要為 0,1,2 執行此操作......用戶有多少。一旦我有了那個作者,我想回傳所有這些作者并洗掉空值。解決這個問題的最簡單方法是什么?
編輯:這是 console.log 字串
{
"collections": [
{
"collection": {
"contract": "atomicassets",
"collection_name": "312443155351",
"name": "Campaigns",
"img": "QmaCxKxtj4vSbVu2HVhSHs2fxYshgLtzmPLCfdPhQvGNSd",
"author": "campaigns",
"allow_notify": true,
"authorized_accounts": [
"campaigns"
],
"notify_accounts": [],
"market_fee": 0.01,
"data": {
"img": "QmaCxKxtj4vSbVu2HVhSHs2fxYshgLtzmPLCfdPhQvGNSd",
"name": "Campaigns",
"description": "Proton Marketing Campaigns 2D NFTs"
},
"created_at_time": "1644873445000",
"created_at_block": "114487854"
},
"assets": "6"
},
{
"collection": {
"contract": "atomicassets",
"collection_name": "354415534331",
"name": "Proton Gifts",
"img": "QmPfnohRCDs2ZGq3ZchKKVDDv4GwzZG2TDuiVrmCDaDHcr",
"author": "nftsairdrop",
"allow_notify": true,
"authorized_accounts": [
"nftsairdrop"
],
"notify_accounts": [],
"market_fee": 0.1,
"data": {
"img": "QmPfnohRCDs2ZGq3ZchKKVDDv4GwzZG2TDuiVrmCDaDHcr",
"name": "Proton Gifts",
"description": "A little gift from me to you in the Protoverse!"
},
"created_at_time": "1640415233000",
"created_at_block": "105572339"
},
"assets": "1"
}
],
"templates": [
{
"collection_name": "312443155351",
"template_id": "74074",
"assets": "3"
},
{
"collection_name": "312443155351",
"template_id": "74112",
"assets": "3"
},
{
"collection_name": "354415534331",
"template_id": "44708",
"assets": "1"
}
],
"assets": "7"
}
uj5u.com熱心網友回復:
這把作者拉出來了
const bcOwner = {
"collections": [
{
"collection": {
"contract": "atomicassets",
"collection_name": "312443155351",
"name": "Campaigns",
"img": "QmaCxKxtj4vSbVu2HVhSHs2fxYshgLtzmPLCfdPhQvGNSd",
"author": "campaigns",
"allow_notify": true,
"authorized_accounts": [
"campaigns"
],
"notify_accounts": [],
"market_fee": 0.01,
"data": {
"img": "QmaCxKxtj4vSbVu2HVhSHs2fxYshgLtzmPLCfdPhQvGNSd",
"name": "Campaigns",
"description": "Proton Marketing Campaigns 2D NFTs"
},
"created_at_time": "1644873445000",
"created_at_block": "114487854"
},
"assets": "6"
},
{
"collection": {
"contract": "atomicassets",
"collection_name": "354415534331",
"name": "Proton Gifts",
"img": "QmPfnohRCDs2ZGq3ZchKKVDDv4GwzZG2TDuiVrmCDaDHcr",
"author": "nftsairdrop",
"allow_notify": true,
"authorized_accounts": [
"nftsairdrop"
],
"notify_accounts": [],
"market_fee": 0.1,
"data": {
"img": "QmPfnohRCDs2ZGq3ZchKKVDDv4GwzZG2TDuiVrmCDaDHcr",
"name": "Proton Gifts",
"description": "A little gift from me to you in the Protoverse!"
},
"created_at_time": "1640415233000",
"created_at_block": "105572339"
},
"assets": "1"
}
],
"templates": [
{
"collection_name": "312443155351",
"template_id": "74074",
"assets": "3"
},
{
"collection_name": "312443155351",
"template_id": "74112",
"assets": "3"
},
{
"collection_name": "354415534331",
"template_id": "44708",
"assets": "1"
}
],
"assets": "7"
}
const authors = bcOwner.collections.map(({collection})=>collection.author)
console.log('authors',authors)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/453642.html
標籤:javascript 数组 反应 目的 下一个.js
