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

有没有人知道如何过滤Node.js中的JSON数据?

我从Ubidots获取传感器数据,但我只想从Ubidots获取最新的“value:”,而不是整个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 }] 

非常感谢您的帮助。

您可以使用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});