i have text file, with this pattern. list of number that were separated with enter or \n
. at end of file there is lot of enter
1
2
3
... //my main is enter
when i read this file whit this code and log the contents, output is and typeof is string:
var contents = fs.readFileSync('./myFile', 'utf8');
1
2
3
...
then i would like this data split with \n
and convert this string to array. using this code. but finally answer is :
contents.split("\n")
have data : [1, 2, 3, '', '', '', '']
var listOfNationalCode = contents.split("\n").map(function (n) {
if (n != '') {
return n;
}
});
console.log(listOfNationalCode);
[ '1',
'2',
'3',
undefined,
undefined,
undefined,
undefined ]
What should I do now for not return undefined
in this code? and i have [1, 2, 3]