[Ethereum] Splice Struct Entry by Index in Solidity

arrayscontract-developmentsoliditystruct

I have a struct and I'd like to remove an item by index:

struct Payout {
  address addr;
  uint yield;
}

Payout[] public payouts;

I'm not familiar with structs and more use to dealing with arrays in javascript where I would do something like this:

payouts.splice(index, 1);

What's the best way to do this with a struct array in Solidity?

Edit: Would also like array to shift down like with the splice function. E.g. when you delete payouts[0] the data from payouts[1] shifts back into its spot.

Best Answer

You can simply do:

function deletePayoutYield() {
    delete payout.yield;
}

Or in a array:

function deletePayoutYield(uint index) {
    delete payoutArray[index].yield;
}

function deletePayout(uint index) {
    delete payoutArray[index];
}
Related Topic