以下是每几分钟10台设备的记录。我需要为每个 id 返回唯一的记录集,并且每个 id 都应该只是最新的。
我怎样才能用 Elasticsearch 或任何其他解决方案来做到这一点?
{
{id: 1, time: 12345},
{id: 2, time: 12346},
{id: 1, time: 12347},
{id: 2, time: 12348},
{id: 1, time: 12349},
{id: 3, time: 12350},
... // 10 different ids and 10000 records
}
最佳答案
这是我所能得到的最接近的结果。如果您在原始数据集中有超过 10 个唯一 ID,则不确定如何选择返回哪 10 个结果。
var rawData = [
{id: 1, time: 12345},
{id: 2, time: 12346},
{id: 1, time: 12347},
{id: 2, time: 12348},
{id: 1, time: 12349},
{id: 3, time: 12350},
{id: 4, time: 12351},
{id: 2, time: 12352},
{id: 7, time: 12353},
{id: 5, time: 12354},
{id: 3, time: 12355},
{id: 6, time: 12356},
{id: 3, time: 12357},
{id: 7, time: 12358},
{id: 6, time: 12359},
{id: 9, time: 12360}
]
var maxSet = {};
// Get all of the max values
rawData.forEach(function (currentValue, index, array) {
var currentMax = maxSet[currentValue.id] || null;
if(currentMax) {
if(currentValue.time > currentMax){
maxSet[currentValue.id] = currentValue.time;
}
} else {
maxSet[currentValue.id] = currentValue.time;
}
});
console.log(maxSet);
// Convert back to object if necessary
var keys = Object.keys(maxSet);
var resultObjs = [];
for(var key in keys) {
resultObjs.push({id: keys[key], time: maxSet[keys[key]]});
}
console.log(resultObjs);
关于javascript - 如何过滤带有多个参数的json对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598901/