Skip to main content
deleted 126 characters in body
Source Link

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Edit:

I could think of the following solution however it is not ideal because it creates empty records in the array:

    function getTripsToAddress() public view returns (Trip[] memory) {
        Trip[] memory tripsForAddress = new Trip[](tripsArray.length);
          for (uint i = 0; i < tripsArray.length; i++) {
              if (tripsArray[i].creator == msg.sender) {
                  tripsForAddress[i] = (Trip({ id: tripsArray[i].id, creator: tripsArray[i].creator, createdAt: tripsArray[i].createdAt, destination: tripsArray[i].destination}));tripsArray[i];
              }
              }
              return tripsForAddress;
    }

Thanks

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Edit:

I could think of the following solution however it is not ideal because it creates empty records in the array:

    function getTripsToAddress() public view returns (Trip[] memory) {
        Trip[] memory tripsForAddress = new Trip[](tripsArray.length);
          for (uint i = 0; i < tripsArray.length; i++) {
              if (tripsArray[i].creator == msg.sender) {
                  tripsForAddress[i] = (Trip({ id: tripsArray[i].id, creator: tripsArray[i].creator, createdAt: tripsArray[i].createdAt, destination: tripsArray[i].destination}));
              }
              }
              return tripsForAddress;
    }

Thanks

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Edit:

I could think of the following solution however it is not ideal because it creates empty records in the array:

    function getTripsToAddress() public view returns (Trip[] memory) {
        Trip[] memory tripsForAddress = new Trip[](tripsArray.length);
          for (uint i = 0; i < tripsArray.length; i++) {
              if (tripsArray[i].creator == msg.sender) {
                  tripsForAddress[i] = tripsArray[i];
              }
              }
              return tripsForAddress;
    }

Thanks

added 656 characters in body
Source Link

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Edit:

I could think of the following solution however it is not ideal because it creates empty records in the array:

    function getTripsToAddress() public view returns (Trip[] memory) {
        Trip[] memory tripsForAddress = new Trip[](tripsArray.length);
          for (uint i = 0; i < tripsArray.length; i++) {
              if (tripsArray[i].creator == msg.sender) {
                  tripsForAddress[i] = (Trip({ id: tripsArray[i].id, creator: tripsArray[i].creator, createdAt: tripsArray[i].createdAt, destination: tripsArray[i].destination}));
              }
              }
              return tripsForAddress;
    }

Thanks

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Thanks

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Edit:

I could think of the following solution however it is not ideal because it creates empty records in the array:

    function getTripsToAddress() public view returns (Trip[] memory) {
        Trip[] memory tripsForAddress = new Trip[](tripsArray.length);
          for (uint i = 0; i < tripsArray.length; i++) {
              if (tripsArray[i].creator == msg.sender) {
                  tripsForAddress[i] = (Trip({ id: tripsArray[i].id, creator: tripsArray[i].creator, createdAt: tripsArray[i].createdAt, destination: tripsArray[i].destination}));
              }
              }
              return tripsForAddress;
    }

Thanks

Source Link

Return an array of structs for a specific condition

I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

contract Trips {
    struct Trip {
        bytes20 id;
        address creator;
        uint256 createdAt;
        string destination;
    }

    Trip[] public tripsArray;
    
    function addTrip (string memory _destination) external {
        bytes20 id = bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        Trip memory trip = Trip({
            id: id,
            creator: msg.sender,
            createdAt: block.timestamp,
            destination: _destination
        });
        tripsArray.push(trip);
    }
}

How could I construct a function that would return an array of trips that a function caller address has created?

Thanks