I am trying to split an array of strings, called 'vertices' and store it as an array of floats.
Currently the array of strings contains three elemets: ["0 1 0", "1 -1 0", '-1 -1 0"]
What I need is an array of floats containing all these digits as individual elements: [0, 1, 0, 1, -1, 0, -1, -1, 0]
I used the split() function as follows:
for(y = 0; y < vertices.length; y++)
{
vertices[y] = vertices[y].split(" ");
}
...which gives me what looks to be what I am after except it is still made up of three arrays of strings.
How might I use parseFloat() with split() to ensure all elements are separate and of type float?
You can use
Array.prototype.reduce
method for this:Instead of
.map(Number)
you can use.map(parseFloat)
of course if you need.Or even shorter: