-4

the line below will return some numbers in the format

8458.268,19166.142,13113.780,25837.795,13113.780,25837.795...

output=output.split("\n").filter(/./.test, /Coordinates/).join("\n").replace(/[^0-9.,]/g,'');

then it outputs in the line below... but before it outputs... I need to perform some calculations... like divide all those numbers by X

document.getElementById('inputTextToSave').innerHTML= output0 + output + output2;
4
  • 2
    Not clear what you're asking. Commented Apr 30, 2017 at 10:53
  • the numbers in an array need to be divided by 2.2 before output.. I don't know to create an array to perform the calculation Commented Apr 30, 2017 at 10:55
  • Post how the raw data really look like. Commented Apr 30, 2017 at 10:58
  • text file contains a line with: Coordinates: (999.000,111.000,234.00,234.000....) Commented Apr 30, 2017 at 11:00

1 Answer 1

0

Try using a map function on the array at the point where you need to make the calculations. Once you have the array so that each item is in the format that you want, you can map through the array and divide each item by 2.2.

E.g.

arr.map((item) => {
  return item/2.2;
}) // at this point you could chain on 'join("\n")' or whatever

If I'm understanding you correctly, you want to make an array out of the data you have, then perform a calculation on each piece of that data, and then do something else with it. If so, then the built in javascript map function is the way to go.

Note: I use ES6 fat arrow syntax for the function inside map, obviously you could just use an anonymous function with map(function(item) {...});

Sign up to request clarification or add additional context in comments.

14 Comments

I tried using this but it will return NaN... var array = ['output']; divBy2 = v => v / 2.2; result = array.map(divBy2);
try running parseFloat() on your item before dividing by 2.2. Eg divBy2 = v => parseFloat(v) / 2.2;
it works now and returns a value... but it only returns the first value from an array. i.e my array consists of 1,2,3,4,5,... it only returns the value of the first number.
What does your code look like now? It's working for me: arr = [100, 200,34,2.2,65]; then running: arr.map((item) => { return item/2.2 }); results in [45.45, 90.90, 15.45, 1, 29.54]. (note: I took out some of the decimals places)
number actually come from a text file... I'm using output below to extract.. var array = [output]; divBy2 = v => parseInt(v) / 2; result = array.map(divBy2);
|

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.