如何在Node.js中过滤JSON数据

时间:2017-08-24 08:36:18

标签: javascript json node.js

有谁知道如何在Node.js中过滤JSON数据?

我从 Ubidots 获取传感器数据,但我只想要最新的"值:"来自Ubidots,而不是整个JSON数据列表。

Node.js代码

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

输出数据

[{ 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 }]

这就是我想要展示的内容

我只想过滤到最新的值,如下所示。

[{ value: 30 }]

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用Array#Reduce获取最高价值。



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