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

156
Visualizações
js: get colum of array of objects

I need your help to get data out of a database. I use sql.js and the db.exec delivers an object res:

const res = db.exec('select time_,temp  from sensors where topic=\"/xx/sensors\" limit 10')

res looks like this:

Array [ {…} ]
​
    0: Object { columns: (2) […], values: (10) […] }
​​
        columns: Array [ "time_", "temp" ]
​​​
            0: "time_"
​​​
            1: "temp"
​​​
            length: 2
​​​
​​
        values: Array(10) [ (2) […], (2) […], (2) […], … ]
​​​
            0: Array [ "23:53:01", 19.51 ]
​​​
            1: Array [ "23:55:01", 19.51 ]
​​​         
            2: Array [ "23:57:01", 19.5 ]
​​​
            3: Array [ "23:59:01", 19.48 ]
​​​
            4: Array [ "00:05:04", 19.47 ]
​​​
            5: Array [ "00:07:04", 19.45 ]
​​​
            6: Array [ "00:09:04", 19.43 ]
​​​
            7: Array [ "00:11:04", 19.43 ]
​​​
            8: Array [ "00:13:04", 19.36 ]
​​​
            9: Array [ "00:15:04", 19.38 ]
​​​
            length: 10
​​​

res is an object with arrays that has columns. How can I get the time_ to look like this:

const labels = ['00:13:04', '00:15:04', ...];

and the temp in this format:

data = [19.36, 19.38, ...];

Can you please help me? regards

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Example, res is as following:

let res = [['1', 1], ['2', 2]]

Then, your required values would be:

let labels = res.map(arr => arr[0]);
let data = res.map(arr => arr[1]);
about 4 years ago · Juan Pablo Isaza Relatório

0

Your result is an array with a single object in it which in turn has two properties columns and values. It looks like you are wanting to map the values array of this object to two individual arrays by index so you will need to access that property appropriately before mapping:

const res = [{ columns: [], values: [] }];

const values = res[0].values;
// or with destructuring
const [{ values }] = res;

You can then either .map() over this array twice

const res = [
  {
    columns: ['time_', 'temp'],
    values: [['23:53:01', 19.51], ['23:55:01', 19.51], ['23:57:01', 19.5], ['23:59:01', 19.48], ['00:05:04', 19.47], ['00:07:04', 19.45], ['00:09:04', 19.43], ['00:11:04', 19.43], ['00:13:04', 19.36], ['00:15:04', 19.38],],
  },
];

const values = res[0].values;

const labels = values.map((arr) => arr[0]);
const data = values.map((arr) => arr[1]);

console.log('labels: ', labels);
console.log('data: ', data);

or use a single pass in a for loop (here using for...of and destructuring each tuple)

const res = [
  {
    columns: ['time_', 'temp'],
    values: [['23:53:01', 19.51], ['23:55:01', 19.51], ['23:57:01', 19.5], ['23:59:01', 19.48], ['00:05:04', 19.47], ['00:07:04', 19.45], ['00:09:04', 19.43], ['00:11:04', 19.43], ['00:13:04', 19.36], ['00:15:04', 19.38],],
  },
];

const labels = [];
const data = [];

for (const [label, datum] of res[0].values) {
  labels.push(label);
  data.push(datum);
}

console.log('labels: ', labels);
console.log('data: ', data);

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