Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

destructuring and returning data found in odd structure from API request

Having a tough time with this one. I’ve been searching throughout but don’t seem to find a similar scenario…

I’m trying to find the author of every collection that a user has.

I’ve made this function to pull data from an api, a specific user that in this case is proton as seen at the end of the link:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;

The thing is that when i consol.log bcOwner , author is inside of an array(collection) which is inside of another array (0), which is inside of an object (collections)… So I need to go inside collections and then inside every 0, followed by collection to get the author (this sentence wasn’t needed but I typed it out to make sure I get what I’m doing lol). And I need to do this for 0,1,2… how ever many the user has. Once I have that author, I want to return all those authors and remove the null. What’s the simplest way to tackle this ?

edit: Here is the console.log string

{
  "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"
}

>Solution :

This pulls out the authors

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading