0

I am working with truffle and failing to get the actual values from my return:

[ [ { [String: '10'] s: 1, e: 1, c: [Object] },
    { [String: '35'] s: 1, e: 1, c: [Object] },
    { [String: '40'] s: 1, e: 1, c: [Object] },
    { [String: '50'] s: 1, e: 1, c: [Object] },
    { [String: '60'] s: 1, e: 1, c: [Object] } ],
  [ { [String: '20'] s: 1, e: 1, c: [Object] },
    { [String: '35'] s: 1, e: 1, c: [Object] },
    { [String: '40'] s: 1, e: 1, c: [Object] },
    { [String: '50'] s: 1, e: 1, c: [Object] },
    { [String: '60'] s: 1, e: 1, c: [Object] } ] ]

I tried result.valueOf()[0][0].toString() but that didn't do the trick.

However, result.valueOf()[0].toString() is working on the following example:

[ [ { [String: '10'] s: 1, e: 1, c: [Object] } ],
  [ { [String: '20'] s: 1, e: 1, c: [Object] } ] ]

Any help would be appreciated!

EDIT: The goal is to use the values for a test, ideally somehow like that:

 assert.equal(result.valueOf()[0][0].toString(), [10, 35, 40, 50, 60]);
 assert.equal(result.valueOf()[1][0].toString(), [20, 35, 40, 50, 60]);

EDIT2: The values that the contract returns are (uint256[], uint256[])

Cheers! carbee

  • Just out of curiosity, can you also post how the response is being returned in the contract? – ReyHaynes Feb 6 '18 at 17:01
  • Yep, updated it in my question! – carbeer Feb 6 '18 at 17:05
1

If you are just expecting an array return, try console.logging: result.args.c, result.args.c[0]

The actual response usually ends up in c.


Update #1

If you were getting a return of arrays, then your overall response should be something like (with BigNumber instead of String):

[ [ BigNumber { s: 1, e: 0, c: [Array] },
    BigNumber { s: 1, e: 0, c: [Array] } ],
  [ BigNumber { s: 1, e: 0, c: [Array] },
    BigNumber { s: 1, e: 0, c: [Array] } ] ]

From that, you have to map the array in order to make the array desired:

let arr = result.map(res => {
  return res.map(res => res.c[0])
})

You should get the array you want with arr.

Since c in your case is an [Object], you'd have to see what is in that object and replace res.c[0] with the map to that object value you want. I assume its .value...but I don't have code to verify that right now. Hopefully this helps.

And just for reference result.valueOf()[0][0].toString() would actually be result.valueOf()[0].c[0].toString().

| improve this answer | |
  • I'd like to get two arrays: [10,35,40,50,60] and [20,35,40,50,60] or all values independently. I'll update my question! – carbeer Feb 6 '18 at 16:55
  • @carbee Updated. – ReyHaynes Feb 6 '18 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.