loops - skipping 2 lines that comes back periodically when parsing javascript -
i'm trying parse document contains 500 times snippet of 258 lines:
256 (first line) atoms. timestep: 66000 1 0.702825 2.71217 2.71612 1 16.9592 2.64886 6.79019 1 0.681418 2.68359 10.8911 1 16.96 2.6822 14.9396 1 0.659922 6.77858 2.72442 1 16.9873 6.7709 6.77907 1 0.676686 6.76797 10.8581 ... 1 16.9295 6.74348 14.9315 (258th line) so i'm trying ignore first 2 lines come periodically, have values want in array. here code it:
fetch('petit_film_256_atomes.txt').then(response => response.text()).then(text => { var linestart = 0; var lineend = 258; (var j = linestart; j < lineend; j++) { const positions = text.split('\n') .map(line => line.trim()) .slice(2) .map(line => line.split(' ')) .map(([size, x, y, z]) => ({ size: number(size), x: number(x), y: number(y), z: number(z) })); } linestart += 258; lineend += 258; }); i thought using linestart , lineend variables first 2 lines every 258 lines can cut using slice(2), first 2 lines of whole document cut. how make slice repeat itself?
this should work.
const size = 258; let result = []; fetch('petit_film_256_atomes.txt') .then(response => response.text()) .then((input) => { // put every line array const arr = input.split('\n') .map(line => line.trim()) while (arr.length > 0) result.push( arr .splice(0, size) // split array chunks of length 258 .slice(2) // remove first 2 lines of every array .map(line => { const [size, x, y, z] = line.split(' ').map(number) return { size, x, y, z } }) ); })
Comments
Post a Comment