Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda