Skip to content

Instantly share code, notes, and snippets.

@brynbellomy
Last active July 11, 2017 22:09
Show Gist options
  • Save brynbellomy/7676ba4c980d54a20258dd1832a01393 to your computer and use it in GitHub Desktop.
Save brynbellomy/7676ba4c980d54a20258dd1832a01393 to your computer and use it in GitHub Desktop.
medium-return-struct-public-func-4.js
const Project = artifacts.require('./Project.sol')
const project = await Project.deployed()
const people = await project.getPeople([ 2, 5 ], { from: accounts[0] })
console.log('people =', people)
// this will print something like:
// people = [ ['0xdeadbeef', '0xabcdeff'], [123, 789] ]
//
// as you can see, we get back an array. each element of this array
// represents a field in the struct type. because we've returned
// many structs, each field has many values. let's reassemble these
// into a more familiar (and more usable) format.
// we can figure out how many structs were returned by checking the
// length of any of the arrays that were returned. they should all
// have the exact same length.
const numPeople = people[0].length
// for clarity's sake, let's define some constants so that we can see
// which field array we're accessing:
const FIELD_ADDR = 0
const FIELD_FUNDS = 1
let peopleStructs = []
for (let i = 0; i < numPeople; i++) {
const person = {
addr: people[FIELD_ADDR][i],
funds: people[FIELD_FUNDS][i],
}
peopleStructs.push(person)
}
console.log('peopleStructs =', peopleStructs)
// this will print something like:
// peopleStructs = [
// { addr: '0xdeadbeef', funds: 123 },
// { addr: '0xabcdeff', funds: 789 }
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment