0

I'm looking to convert a nested array of the type string to type float, or alternatively parsing it from a text file. Format is something along the lines of this [45.68395, 32.98629],[23.6777, 43.96555],[43.66679, 78.9648]

2
  • 2
    JSON.parse, maybe? Commented Jul 30, 2018 at 9:56
  • Please visit the help center, take the tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. Commented Jul 30, 2018 at 9:59

4 Answers 4

1

The first step would be to create valid JSON from your string.

If your input will always follow the schema you showed us, you could just prepend and append brackets to the string. This is not a pretty solution though. You should first check if you can get valid JSON in the first place.

A solution could look like this, provided that the input string will always follow the format of "[float, float], [float, float]":

const input = "[45.68395, 32.98629],[23.6777, 43.96555],[43.66679, 78.9648]";

// Add brackets in order to have valid JSON.
const arrayString = "[" + input + "]"; 

// Parse the string into an object.
const parsedArray = JSON.parse(arrayString);

// Flatten the nested array to get a one dimensional array of all values.
var flattenedArrays = [].concat.apply([], parsedArray);

// Do something with your values.
flattenedArrays.forEach(floatValue => console.log(floatValue));
Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSON.parse, if your numbers are actually numbers in a JSON (serialized without quotes).

let test = "[[3, 4.2], [5, 6]]";
let test2 = JSON.parse(test);
console.log(test2);

Otherwise you can simply convert your array of array of strings to array of array of numbers using + and some array mapping. :

let test = [["3", "4.2"], ["5", "6"]];
let test2 = test.map((x) => x.map((y) => +y));
console.log(test2);

Of course, you can combine both solutions if for some reason you don't control the input and have a JSON containing strings.

Comments

0

This thread shows you how to loop through an array of strings to convert it to an array of floats.

3 Comments

Please explain it in a few words here.
links only answer are not usually considered valid answer on SO. If you think the question is already answered in the linked question you suggested, consider flagging the question as duplicate (click 'flag' under question, copy link in the search bar)
Good to know I will do!
0

i hope this will work..

var input = [[45.68395, 32.98629],[23.6777, 43.96555],[43.66679, 78.9648]]

var output = [];
  input.forEach(o => {
    o.forEach(s => parseFloat(s))
    output.push(o);
})

console.log(output);

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.