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
  • 1
    Does this answer your question? How to convert a string to an integer in JavaScript? Commented Nov 13, 2020 at 20:37
  • 1
    Just change Oracle to do the map: this.endpnt = endpnt.map(i => +i); Commented Nov 13, 2020 at 20:38
  • I took everyone's answers into consideration for different variations to solve this issue thank you. Commented Nov 13, 2020 at 21:31

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Destructuring/Reaggregating the array you use as the second parameter to the oracle constructor is useless.
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
@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
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
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)

1 Comment

Your code also was able to fix my issue thank you. @maioman
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) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.