Reputation: 89
When I use numbers in the example below, I get the correct return/output.
var array = [1,2,4,5,];
divBy2 = v => v / 2;
var result = array.map(divBy2);
When I try this below, I get NaV.
var array = [output];
divBy2 = v => v / 2;
var result = array.map(divBy2);
My code is written as below. Basically, I'm loading a text file with the following line:
Numbers (999.000,999.000),(999.000,999.000),
Then I want to create an array and take those numbers and divide by 2. To return just the numbers without the entire string.
var reader = new FileReader();
function readText(that){
if(that.files && that.files[0]){
var reader = new FileReader();
reader.onload = function (e) {
var output=e.target.result;
output=output.split("\n").filter(/./.test,/Numbers/).join("\n").replace(/[^0-9.,]/g,'');
var array = [output];
divBy2 = v => v / 2;
var result = array.map(divBy2);
document.getElementById('inputTextToSave').innerHTML= output;
Upvotes: 2
Views: 191
Reputation: 15576
"1,2,3"
) instead.let output = "1,2,3";
var array = [output];//doesnt work
//this creates ["1,2,3"]
//array[0] === "1,2,3"
console.log("array", array);
var array2 = output.split(",");//works but creates string array
//this creates ["1","2","3"]
//array2[0] === "1"
//array2[1] === "2"
//array2[2] === "3"
console.log("array2", array2)
var array3 = output.split(",").map(Number);//works and converts to number
//this creates [1,2,3]
//array2[0] === 1
//array2[1] === 2
//array2[2] === 3
console.log("array3", array3)
//The closest to your attemp is eval() which shouldnt be used in normal cases
var array4 = eval("[" + output + "]");
console.log(array4)
output
is a comma separated string in your code (999.000,999.000,999.000,999.000,999.000,999.000,999.000,999.000,999.000
), split it into an array using String.prototype,splitlet output = `Numbers (999.000,999.000),(999.000,999.000),
Numbers (999.000,999.000),(999.000,999.000),
Numbers (999.000,999.000),(999.000,999.000),
Numbers (999.000,999.000),(999.000,999.000)`;
output=output.split("\n").filter(/./.test,/Numbers/).join("\n").replace(/[^0-9.,]/g,'');
console.log(output);
let array = output.split(",");
console.log(array);
let divBy2 = v => v / 2;
let result = array.map(divBy2);
console.log(result);
To take a look at your code:
let output = "1, 2, 3";
var array = [output];
console.log(array);//["1,2,3"]
divBy2 = v => v / 2;
var result = array.map(divBy2);
console.log(result);//[NaN] as "1,2,3"/2 =NaN (not a number)
Upvotes: 1
Reputation: 4322
You're likely getting an error because map
is trying to apply itself to an array rather than a number.
Don't use array=[output]
. If output is already an array, then you would get something like this [[3,5,656,34,23]]
, and the map function would then be trying to divide the entire inner array by 2, rather than each element.
array=output
should do the trick, further, you could just map over output, like var result = output.map(divBy2);
Upvotes: 0
Reputation: 304
There's small a mistake in your code.
output=output.split(............);
the above line of code will return you an array. So you could simply do this
var array = output;
or else you can define the variable at once
var array=output.split(............);
Upvotes: 0