gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:38:26 25 4
gpt4 key购买 nike

有谁知道如何在 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 }]

非常感谢您的帮助。

最佳答案

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

关于javascript - 如何在 Node.js 中过滤 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com