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

126
Views
Can I get the result in a Array instead of JSON array from sqlite3 database in node?

I am really new to this and I just got a grasp of json data and I want it as a regular array

loginAndDetails.get(`SELECT * FROM logTB` , (err,data) => {
   console.log(data)
})

//output might be 1d or 2d
  {
    entry1: 'data1',
    entry2: 'data2',
    .,    
    .,
    entryN: 'dataN'
  }


loginAndDetails.all(`SELECT * FROM logTB` , (err,data) => {
   console.log(data)
})
// alternative output:
  [{
    entry1: 'data1',
    entry2: 'data2',
    .,    
    .,
    entryN: 'dataN'
  },
{
    entry1: 'data1',
    entry2: 'data2',
    .,   
    .,
    entryN: 'dataN'
  }]

but I have already created all my function to work with a normal JS array like this

['data1','data2',...,'dataN'] 

(or) 

[['data1','data2',...,'dataN'],
['data1','data2',...,'dataN']]

so is there a way to convert all the data dynamically because Some times it might be a 2d array some times it might be a 1d array and there might be 'n' number of objects

do I have to create a function to build this or is there any other method

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this, it will give you an array of arrays.

var finalResult = [];

// checking if data is an array of objects
if (Array.isArray(data)){
    for (var i=0; i < data.length; i++ ){
        console.log(data[i]);
        var result = getFlatArray(data[i]);
        finalResult.push(result);
    }
    console.log(finalResult);
}
else{
    finalResult = getFlatArray(data);
    console.log(finalResult);
}

function getFlatArray(data){
    var result = [];
    for(var j in data){
        result.push([data[j]]);
    }
   return result;
}

But if you want to access your JSON objects you can do as follows.]

For 1d:- data.entry1

For 2d:- data is just a javascript array so you can traverse it with a normal loop and access the objects inside the loop with the array index.

for(var i=0; i < data.length; i++){
    // access the data in the loop using array index
    data[i].entry1;
}
about 4 years ago · Juan Pablo Isaza Report
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!