Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

Javascript: Create array from two columns in .cvs file

I am very new at JS, here is my target & my problem:

I have a ".cvs" file, and need to select two columns, and create an Array with that two columns.

1. CVS File:

col1    col2    col3    col4    col5    col6    col7    col8    col9    col10
-1  6/10/2020   08:35:43    ?45.64172279    N   ?10.19579398    E   198.2   M   4
-1  6/10/2020   08:35:44    45.64193714 N   ?10.1958776 E   198.19  M   4
-1  6/10/2020   08:35:43    45.64220345 N   ?10.19598908    E   198.2   M   4
-1  6/10/2020   08:35:44    45.6423983  N   ?10.19606341    E   198.19  M   4
-1  6/10/2020   08:35:43    45.6429504  N   ?10.19632354    E   198.2   M   4
-1  6/10/2020   08:35:44    45.64329464 N   ?10.19658367    E   198.19  M   4
-1  6/10/2020   08:35:43    45.64341805 N   ?10.19758703    E   198.2   M   4
-1  6/10/2020   08:35:44    45.64339856 N   ?10.19838601    E   198.19  M   4
-1  6/10/2020   08:35:43    45.64313876 N   ?10.1987855 E   198.2   M   4
-1  6/10/2020   08:35:44    45.64244377 N   ?10.19869259    E   198.19  M   4
-1  6/10/2020   08:35:43    45.6418527  N   ?10.19879479    E   198.2   M   4
-1  6/10/2020   08:35:44    45.6415669  N   ?10.19715967    E   198.19  M   4
-1  6/10/2020   08:35:44    45.64170331 N   ?10.19648147    E   198.19  M   4
0   7/10/2020   09:35:44    45.64189167 N   ?10.19615631    E   198.19  M   4

2. Read .CVS and select the columns:

const response = await fetch('schio_000_orig_1.cor');
                const data = await response.text();
                // console.log(data);

                const table = data.split('
').slice(1);
                table.forEach(row => {
                    const colums = row.split('');
                    const lat = colums[3];
                    const lng = colums[5];
                    console.log(lat, lng)

Output by now...

enter image description here

3. Desired Output

var latlng = [
                        // array
                        [?45.64172279, 10.19579398],
                        [?45.64193714, 10.1958776],
                        [?45.64220345, 10.19598908],
                        [?45.6423983, 10.19606341],
                        [?45.6429504, 10.19632354],
                        [?45.64329464, 10.19658367],
                        [?45.64341805, 10.19758703],
                        [?45.64339856, 10.19838601],
                        [?45.64313876, 10.1987855],
                        [?45.64244377, 10.19869259],
                        [?45.6418527, 10.19879479],
                        [?45.6415669, 10.19715967],
                        [?45.64170331, 10.19648147],
                        [?45.64189167, 10.19615631],
                    ];

- Cant find the way to achieve it. I have tried push, concat, JSON.PARSE and still not getting the right array...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

Your code has the right idea, you just need to add the array to an array. Two ways of doing it would be

Using push:

const out = []
table.forEach(row => {
  const colums = row.split('');
  const lat = colums[3];
  const lng = colums[5];
  out.push([lat, lng]);
});

Using map:

const out = table.map(row => {
  const colums = row.split('');
  const lat = colums[3];
  const lng = colums[5];
  return [lat, lng];
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...