Answers for "Solidity add to array"

1

Solidity add to array

// Create array
address[] addresses
uint256[] myArray

// Add to array
function add() internal {
  myArray.push(123); 
}

// Remove from array
function removeUnordered() internal {
  myArray[index] = myArray[myArray.length - 1];
  myArray.pop();
}

function removeOrdered() internal {
  
  // WARN: This unbounded for loop is an anti-pattern
  
  for(uint256 i = index; i < myArray.length-1; i++){
    myArray[i] = myArray[i+1];      
  }
  myArray.pop();
}
Posted by: Guest on May-04-2022
0

solidity append to array

var myContract;
var rowToGet = 0; // 1,2,3
var instance = MyContract.Deployed()
  .then(function(instance) {
    myContract = instance;
    return myContract.myBytesArray(rowToGet);
  })
  .then(function(response) {
    console.log(response); // one element of the array
  });
}
Posted by: Guest on March-23-2022

Browse Popular Code Answers by Language