从 API 请求中解构并返回以奇怪结构发现的数据

穆奇

与这个有一段艰难的时光。我一直在寻找,但似乎没有找到类似的场景......

我正在尝试查找用户拥有的每个集合的作者。

我已经制作了这个函数来从 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"
}
戴夫

这把作者拉出来了

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章