Michael Michael - 1 month ago 17
Node.js Question

How to filter JSON data in Node.js

does anyone know how to filter JSON data in Node.js?

I'm getting sensor data from Ubidots, but I just want the latest "value:" from Ubidots and not the whole list of JSON data.

Node.js Code

var ubidots = require('ubidots');
var client = ubidots.createClient('API Key');

client.auth(function () {
this.getDatasources(function (err, data) {
//console.log(data.results);
});

var v = this.getVariable('Variable Key');

v.getValues(function (err, data) {
console.log(data.results);
});
});


Output Data

[{ timestamp: 1503473215620,
created_at: 1503459283386,
context: {},
value: 30 },
{ timestamp: 1503393988751,
created_at: 1503379656112,
context: {},
value: 30 },
{ timestamp: 1503386506168,
created_at: 1503372174737,
context: {},
value: 26 },
{ timestamp: 1503386398234,
created_at: 1503372098148,
context: {},
value: 26 },
{ timestamp: 1503386202121,
created_at: 1503371960322,
context: {},
value: 22 },
{ timestamp: 1501487126923,
created_at: 1501469129791,
context: {},
value: 25 },
{ timestamp: 1501487121960,
created_at: 1501469127666,
context: {},
value: 25 },
{ timestamp: 1501487116616,
created_at: 1501469121192,
context: {},
value: 25 },
{ timestamp: 1501487111566,
created_at: 1501469118178,
context: {},
value: 25 },
{ timestamp: 1501487106428,
created_at: 1501469109047,
context: {},
value: 25 },
{ timestamp: 1501487101315,
created_at: 1501469103976,
context: {},
value: 25 },
{ timestamp: 1501487096364,
created_at: 1501469098454,
context: {},
value: 25 },
{ timestamp: 1501487091095,
created_at: 1501469094217,
context: {},
value: 25 }]


This is what I just want it to show

I just want it to filter to just the latest value as shown below.

[{ value: 30 }]


Your help is much appreciated.

Answer Source

You can use Array#Reduce to get the highest value.

const data = [{ timestamp: 1503473215620,
    created_at: 1503459283386,
    context: {},
    value: 30 },
  { timestamp: 1503393988751,
    created_at: 1503379656112,
    context: {},
    value: 30 },
  { timestamp: 1503386506168,
    created_at: 1503372174737,
    context: {},
    value: 26 },
  { timestamp: 1503386398234,
    created_at: 1503372098148,
    context: {},
    value: 26 },
  { timestamp: 1503386202121,
    created_at: 1503371960322,
    context: {},
    value: 22 },
  { timestamp: 1501487126923,
    created_at: 1501469129791,
    context: {},
    value: 25 },
  { timestamp: 1501487121960,
    created_at: 1501469127666,
    context: {},
    value: 25 },
  { timestamp: 1501487116616,
    created_at: 1501469121192,
    context: {},
    value: 25 },
  { timestamp: 1501487111566,
    created_at: 1501469118178,
    context: {},
    value: 25 },
  { timestamp: 1501487106428,
    created_at: 1501469109047,
    context: {},
    value: 25 },
  { timestamp: 1501487101315,
    created_at: 1501469103976,
    context: {},
    value: 25 },
  { timestamp: 1501487096364,
    created_at: 1501469098454,
    context: {},
    value: 25 },
  { timestamp: 1501487091095,
    created_at: 1501469094217,
    context: {},
    value: 25 }];
    
const result = data.reduce((acc, curr) => {
  acc = acc.value > curr.value ? acc : curr;
  
  return acc;
}, {});

const {value} = result;

console.log({value});