2

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;
5
  • array=output; you dont want a nested array... also why do you join() if you want an array? Commented May 6, 2017 at 10:20
  • Is this some kind of assignment? stackoverflow.com/questions/43705581/… Commented May 6, 2017 at 10:23
  • By NaV, did you mean NaN? Commented May 6, 2017 at 10:24
  • You get NaN because output is a string, I guess? Commented May 6, 2017 at 10:24
  • Also don't put array = [output] using [] this will put your output array inside another array. Commented May 6, 2017 at 10:24

3 Answers 3

1
  1. You cannot place a string inside array to get the comma separated syntax work, it creates an array with a single string ("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)

  1. 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,split

let 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)

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

2 Comments

thanks a lot for your help... just one more question... my "output" return numbers with many decimal points.. i.e 3.77777,2.455555,83.2333. I only need 3 decimal points. Thanks
@AlexaAtna You can use toFixed(3) which will return a string adjusted to 3 decimal places. in most cases. (Note that approximations will not be perfect for all fractions, if you need more accuracy you will have to look into libraries implementing floating point arithmetic in a more precise way.)
0

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(............);

Comments

0

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);

Comments

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.