1

I'm receiving data from a websocket connection on a page, the problem is that i'm receiving a JSON array of arrays of strings while i need an array of arrays of numbers. Here is a snapshot of the array:

var myArr = 
[["7106.86000000", "0.00255500"],
["7107.34000000", "0.00274200"],
["7107.55000000", "0.11258800"],
["7107.58000000", "0.20000000"],
["7107.67000000", "0.17947000"],
["7107.82000000", "0.08443900"],
["7107.91000000", "0.40000000"],
["7108.00000000", "0.08000000"],
["7108.04000000", "0.00500000"],
["7108.22000000", "0.00200000"],
["7108.31000000", "0.32130200"],
["7108.34000000", "0.32127500"],
]

How can i convert those strings to number? Do i need to use my own function or there is already a built in feature to do that? Thanks in advance!

2 Answers 2

1

You don't need jQuery to achieve this.

const newArray = myArr.map( childArr => childArr.map( value => Number( value ) ) )

It loops through myArr ( an array of arrays ) and then do the same thing on the children. .map() returns a new array.

Sign up to request clarification or add additional context in comments.

Comments

1

var myArr = [
  ["7106.86000000", "0.00255500"],
  ["7107.34000000", "0.00274200"],
  ["7107.55000000", "0.11258800"],
  ["7107.58000000", "0.20000000"],
  ["7107.67000000", "0.17947000"],
  ["7107.82000000", "0.08443900"],
  ["7107.91000000", "0.40000000"],
  ["7108.00000000", "0.08000000"],
  ["7108.04000000", "0.00500000"],
  ["7108.22000000", "0.00200000"],
  ["7108.31000000", "0.32130200"],
  ["7108.34000000", "0.32127500"],
]

const numberArr = myArr.map(x => x.map(y => parseFloat(y)))
console.log(numberArr)

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.