Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda