Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Javascript - use an array to calculate some coordinates

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("
").filter(/./.test, /Coordinates/).join("
").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;
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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("
")' 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) {...});


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...