Home > Back-end >  js: get colum of array of objects
js: get colum of array of objects

Time:11-25

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

CodePudding user response:

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]);

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related