Home > front end >  I can't create a array with objects using map or for
I can't create a array with objects using map or for

Time:04-22

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)).

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

CodePudding user response:

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;
};

  • Related