【发布时间】:2017-08-24 08:36:18
【问题描述】:
有人知道如何在 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 }]
非常感谢您的帮助。
【问题讨论】:
标签: javascript json node.js