I have array of array that looks like this
var data=[["snp","3569#maspd"],["kpr","ant#$casd"]];
What I want to do is to write this data to a file according to length of the [1]element.
For example, let's take first element in array (which is also an array)
["snp","3569#maspd"]
[1] element 3569#maspd is 10 characters long= length=10, so data from this array will be written into file "10.txt"
I have written a code for it
var result=cont.split("\n").
map(function(r){return r.split(",").slice(2,4);//.join(":")
}) //this creates array of arrays
result.forEach(function(x){
var length=x[1].length;
var str=length.toString();
var fil=length+".txt";
var txt=x.join(":");
fs.exists(fil,function(exist){
if(exist){
fs.appendFile(fil,txt,function(err){
if(err) console.log(err)
})
}
else{
fs.writeFile(fil,txt,function(err){
if(err) console.log(err)
})
}
})
}
)
But it always throws an error "cannot read property length of undefined". When I remove all code and leave only
result.forEach(function(x){
var length=x[1].length;
var str=length.toString();
console.log(str)
}
)
It works, did I overlook an bug here? here is an example value of what result array looks like (with random data)
var result=[[ '62346asd5f5510dda8f6223c557bb0bf0b5',
'MH))WXlhs\'uOSu.iwJk[n}oz#w>T6L' ],
[ '00d7994173ds265bfe71182154a1143b0', '&Df1' ],
[ '1b6c32941719fcbfc76c6e5428e5f5',
'Fc/0PG#mHb49&#)V|$Swrr7as_*UnL~Y' ],
[ 'f931c2256eca136c97a9a9af4dcae', '.a=]' ]]