i am writing a code in which i have to create files equal to number of elements in a array and write respective array element in files. for example array = [one,two,three], then i want to have file one with content one and so on.
var inputLength = inputs.length;
inputs.forEach(function(elem) {
i = inputs.indexOf(elem);
console.log(i);
fs.writeFile(Execute.path + Execute.folder + "/inputFile" + i, elem, function(err) {
if (err) {
console.log(err);
} else {
console.log("Input" + i + " file was saved!");
}
});
console.log(i);
});
output of first console.log(i)
is fine i.e. 0,1,2,3 so on but output of second console.log(i)
is always the index of last element.
whats the problem in my code and how to achieve my goal