I have a usecase wherein I am reading streaming data from server, and putting it into an array. So my approach is:
var points = str.split("n"); //split input based on new line character
var positions = new Float32Array();
for (let i = 0; i < points.length; i++) {
var coords = points[i].split("_"); //split line into individual values separated by '_'
for(let j=0; j < coords.length; j++) {
var coord = parseFloat(coords[j]) // parse string to float
positions.push(coord); // add to float array
}
}
But this is not working. code execution just stops at ‘push’, and nothing happens after that.
Is Float32Array not correct data structure for this usecase?
2
Answers
The error is happen because you’re not initialize the size of array. You can change that code to my recommendations.
Or you can just simple change Float32Array to basic array.
Typed arrays don’t have a
push
method. Instead just populate a standard array and then pass that to theFloat32Array
constructor. You could also use some array methods here, likemap
andflatMap
: