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

I can't create a array with objects using map or for

I have a number: totalSupply.

A function to get address based on this number (vjkNFTContract.ownerOf(i))

and another function to get the data, also based on this number (vjkNFTContract.tokenURI(i)).

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

I need to create an array with objects [{…}, {…}]

The size of this array should be totalSupply and each object should contain the address and the data.

I’m trying something like this:

      const structuredCollections = () => {
        for (let i = 0; i < totalSupply; i++) {
          [
            {
              tokenId: i,
              addressSender: vjkNFTContract.ownerOf(i),
              uri: vjkNFTContract.tokenURI(i),
            },
          ];
        }
      };

But I’m pretty sure it’s wrong.. but I cannot find out…
I’m new to Javascript

>Solution :

You need to push the objects onto a variable holding the array, then return the array.

const structuredCollections = (totalSupply) => {
  const result = [];
  for (let i = 0; i < totalSupply; i++) {
    result.push({
      tokenId: i,
      addressSender: vjkNFTContract.ownerOf(i),
      uri: vjkNFTContract.tokenURI(i),
    });
  }
  return result;
};
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