Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
why the order of loop changes by parsing data in sqlite database

an xml document is edited using a function. First, all duplicate files are removed. Then the content is entered into a sql database. For this I run a loop that corresponds to the length of the xml elements (here already converted to json). When I look at the content of the table in the database, I see that the order of the entries does not correspond to the order of the array. So within the database query of my server I printed the run variable i and saw that it starts at 0 but 2,3,1 are appended to the end in this order. Unfortunately I don't understand at all why this happens and hope that someone can help me here.

function extractquestions(filename){
    let questions = [];
    let sql_insert = 'INSERT INTO Fragen (Titel, Frage) values (?,?)';
     
    fs.readFile('./files/'+filename, function(err, data){
        if(err){
            throw err;
        }
        
        const xmlDataStr = data;
        const options = {
            ignoreAttributes: false,
            attributeNamePrefix : "attr_"
        };
        const parser = new XMLParser(options);
        const output = parser.parse(xmlDataStr);
        for(let i = 0; i<output.quiz.question.length; i++){
     
            //Here the needed Questions are filtered out of the xml (Json) data
            if(output.quiz.question[i].attr_type == "coderunner"){
                questions.push(output.quiz.question[i]);
            }
        }


        //Delete Duplicates from array
         const filteredQuestions = questions.reduce((acc, current) => {
            const x = acc.find(item => item.name.text === current.name.text);
            if (!x) {
                return acc.concat([current]);
            } else {
                return acc;
            }
        }, []);
        
        for(let i = 0; i<filteredQuestions.length; i++){
            db.run(sql_insert, filteredQuestions[i].name.text, filteredQuestions[i], (err, result)=>{
                console.log(i);
                if(err){
                    throw err;
                }
            });
        }
    });
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!