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

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

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 Denunciar

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 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