2017-08-24 125 views
1

没有人知道如何过滤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 }] 

您的帮助是非常感谢。

+0

的可能的复制[如何在node.js中筛选JSON数据?](https://stackoverflow.com/questions/25514876/how-to-filter-json-data- in-node-js) – Luca

回答

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

+0

谢谢,兄弟!真的很感激它,它的作品! – Michael