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

160
Views
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 answers
Answer question

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 Report

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