1

Is there a way I can iterate through each Oracle endpnt array and parse the strings to numbers and still keep the Oracle endpnt arrays in order.

Code

var oracleTitle= ['oracle1','oracle2','oracle3','oracle4']
var oracleEndpnt = [['1','3'],['1','2'],['1','3'],[]]


function Oracle(name, endpnt) {
  this.name = name;
  this.endpnt = endpnt

}

var oracles = []

for(var i=0;i<oracleTitle.length;i++) {

  oracles.push(new Oracle(oracleTitle[i],oracleEndpnt[i]))

}

console.log(oracles)

Result

[
  Oracle { name: 'oracle1', endpnt: [ '1', '3' ] },
  Oracle { name: 'oracle2', endpnt: [ '1', '2' ] },
  Oracle { name: 'oracle3', endpnt: [ '1', '3' ] },
  Oracle { name: 'oracle4', endpnt: [] }
]

3 Answers3

1

If I understand correctly this should do:

var oracles = [
  { name: 'oracle1', endpnt: [ '1', '3' ] },
  { name: 'oracle2', endpnt: [ '1', '2' ] },
  { name: 'oracle3', endpnt: [ '1', '3' ] },
  { name: 'oracle4', endpnt: [] }
]

var r = oracles.map(x => {
  x.endpnt = x.endpnt.map(z => Number(z))
  return x
})

console.log(r)
maioman
  • 18,154
  • 4
  • 36
  • 42
1

If you dont want to change all the code, you can run a map for the second array

var oracleTitle= ['oracle1','oracle2','oracle3','oracle4']
var oracleEndpnt = [['1','3'],['1','2'],['1','3'],[]]


function Oracle(name, endpnt) {
  this.name = name;
  this.endpnt = endpnt

}

var oracles = []

for(var i=0;i<oracleTitle.length;i++) {
// Change this part
  oracles.push(new Oracle(oracleTitle[i],oracleEndpnt[i].map(num => parseInt(num))));

}

console.log(oracles)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 1
    Destructuring/Reaggregating the array you use as the second parameter to the oracle constructor is useless. – maioman Nov 13 '20 at 20:39
  • Thank you so much the example I provided was mock data but I used your solution with the .map() for my main issue on a bigger scale and it worked perfectly. If I'm not mistaken we're using the map() function passing it the num parameter that would target each item in the array then parsing each item in the array and creating a new array based on the new results?. I just don't want to plug it in and not understand correct me if I'm wrong. @VictorShinobiGakiya – Kimani Kelly Nov 13 '20 at 21:16
  • @KimaniKelly That's correct, we're mapping here the array that corresponds to the parsed title, in this case since they are the index of the array is the same as the index of the title [i], you won't have a problem. If you were to have a different number of titles / arrays, this method might cause you troubles – Victor Shinobi Gakiya Nov 13 '20 at 23:52
  • 1
    I applied this method inside a for loop that would iterate over 90 arrays to parse multiple bytes32 hex string to a legible string and the outcome worked perfectly. @VictorShinobiGakiya – Kimani Kelly Nov 14 '20 at 00:59
0

var oracleTitle= ['oracle1','oracle2','oracle3','oracle4']
var oracleEndpnt = [['1','3'],['1','2'],['1','3'],[]]
function Oracle(name, endpnt) { this.name = name; this.endpnt = endpnt }

var oracles = oracleTitle.map((s, i) => new Oracle(s, oracleEndpnt[i].map(Number)))

console.log( JSON.stringify(oracles) )
Slai
  • 22,144
  • 5
  • 45
  • 53