Insert data into a Javascript array #NODE ERROR( EMFILE: Too Many open files, open )

Asked

Viewed 67 times

0

I have 3 arrays in my code, INFO, INFOS AND TEST.

And I need to make a function check if the position of my test variable is equal to my info variable and if it is equal, the final variable is: In the case of the test variable in the actual code, I have 167,082 positions. In the case of the first position of the Final array:

would be: 201 - Senior military police officers

of the second: 101 - General officers of the armed forces.

and so on.

var info = ["101", "102", "103", "201"]
var infos = ["Oficiais generais das forças armadas", "Oficiais das forças armadas", "Praças das forças armadas", "Oficiais superiores da polícia militar"]
var teste = ["201", "101", "101", "201", "103", "201", "201"]

   for(i = 0;i < teste.length;i++){
     for(j = 0;j < info.length;j++){
       if(teste[i]==info[j]){
         var final=[]
         final = info[j] + " - " + infos[j]+"\n";
         console.log(final);
         fs.appendFile('resultado.json',final, function (err) {
           if (err) throw err;
         });
       } 
     }
 }

The final file always gives the following error:

EMFILE: Too Many open files, open

And It Closes Saving Only 8189 Objects.

  • What’s the matter, I ran the and printed 201 - Oficiais superiores da polícia militar&#xA;&#xA;101 - Oficiais generais das forças armadas&#xA;&#xA;101 - Oficiais generais das forças armadas&#xA;&#xA;201 - Oficiais superiores da polícia militar&#xA;&#xA;103 - Praças das forças armadas&#xA;&#xA;201 - Oficiais superiores da polícia militar&#xA;&#xA;201 - Oficiais superiores da polícia militar

  • 1

    The problem is that if I run on the console using Node it always hangs on less than 9 thousand mounted objects, in the array info and Infos I have 607 positions. And in the Test array, I have 167,000 objects and I can’t save the actual result that would be 167,000 objects in the final array.

  • I get it. Edit your question and tag Node.js for people who specifically understand Node to be interested in the question. Also put the error message in the question along with this explanation of the problem you just gave me, sometimes people stay only on the question and do not get to read the comments. I can’t help you because I understand JS in the browser but I don’t know anything about Node. But one thing caught my attention you do a file writing inside the for it would not be the case to do this writing only after the two ties for end?

  • I will change to check both your tip and the Node question.

1 answer

2


See if that’s what you want:

var info = ["101", "102", "103", "201"];
var infos = ["Oficiais generais das forças armadas", "Oficiais das forças armadas", "Praças das forças armadas", "Oficiais superiores da polícia militar"];
var teste = ["201", "101", "101", "201", "103", "201", "201"];

function result(compare1, compare2, data) {
    var res = [];
    for(var i in compare1) {
       for (var j in compare2) {
          if (typeof compare1[i] !== 'undefined' && typeof compare2[j] !== 'undefined' && typeof data[i] !== 'undefined' && (compare1[i] == compare2[j])) {
              res.push(compare2[j] + " - " + data[i]+"\n");
          }
       }
    }
   return res;
}

 var res = result(teste, info, infos);
 console.log(res);
  • Ivan, I tested it here! it wasn’t exactly what I wanted, because the res result was changed. But it gave me a north and I adjusted! thank you very much.

  • I got it, baby.

  • Ivan, how would I keep the data from the test vector and only change those that have Infos? In this case, adding those who have information.

  • A condition would no longer solve, like: if(regra) { res.push(novo); } else { res.push(mantem); }?

  • I get it, I’m gonna test it here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.