我在javascript中有两个对象数组:
第一个传感器获得了传感器的重量
{
time : 'some timestamp',
weigth : 1160
}
第二个传感器被另一个传感器获取rfid
{
time : 'some timestamp',
rfid : 'some rfid identifer'
}
对于获得的每个rfid标识符,我想获取采集的数据时间前后的重量。而且,如果我在rfid之后没有得到数组中的任何权重,我将得到一个未定义的值。
两个数组都按时间排序。
现在我有了当前代码:
var rfidNext = [];
for (var i = 0; i < this.acquiredData.rfid.length; i++)
{
var weigths = this.acquiredData.weigthLitter;
var currentRfid = this.acquiredData.rfid[i];
var weigther = weigths.find(w => w.time > currentRfid.time);
if (!weigther)
rfidNext.push(currentRfid);
else
toSend.events.push({
// Some logic here
});
}
这段代码似乎使我获得了rfid数据之后的正确权重,但是我找不到找到之前的正确方法。
编辑:数据示例
[ { time: 1561458881523, value: '1000 0000 0000 0001' },
{ time: 1561458882212, value: '1000 0000 0000 0001' },
{ time: 1561458883941, value: '1000 0000 0000 0002' } ]
[ { time: 1561458881182, value: 2130 },
{ time: 1561458881584, value: 1000 },
{ time: 1561458882789, value: 2130 },
{ time: 1561458884599, value: 3290 } ]
例如,使用那些数据:第一个rfid在获取的第一个和第二个weigth之间获取,这意味着该动物开箱即用,第二个rfid显示该动物回来了,最后一个rfid显示了该动物。体重再高一点,另一个rfid,所以另一只动物进了盒子。
编辑:更大的数据集示例
[ { time: 1561469442605, value: '1000 0000 0000 0001' },
{ time: 1561469463263, value: '1000 0000 0000 0001' },
{ time: 1561469503038, value: '1000 0000 0000 0002' },
{ time: 1561469524312, value: '1000 0000 0000 0001' },
{ time: 1561469530073, value: '1000 0000 0000 0002' },
{ time: 1561469551351, value: '1000 0000 0000 0001' },
{ time: 1561469552085, value: '1000 0000 0000 0001' },
{ time: 1561469579144, value: '1000 0000 0000 0001' },
{ time: 1561469627817, value: '1000 0000 0000 0001' },
{ time: 1561469629525, value: '1000 0000 0000 0002' } ]
[ { time: 1561469394648, value: 1000 },
{ time: 1561469443257, value: 2130 },
{ time: 1561469463268, value: 1000 },
{ time: 1561469503679, value: 2160 },
{ time: 1561469524962, value: 3290 },
{ time: 1561469530073, value: 2130 },
{ time: 1561469551358, value: 1000 },
{ time: 1561469552740, value: 2130 },
{ time: 1561469579144, value: 1000 },
{ time: 1561469628430, value: 2130 },
{ time: 1561469630175, value: 3290 } ]
详细说明:
开始时没有动物,体重为1000
然后动物1进来:2130的体重
动物1出来:体重为1000
动物2进入:体重:2160
动物1进入:3290体重
以此类推...
所以我的目标是根据体重变化和rfid读数来发现动物是进入室内还是室外。
答案 0 :(得分:1)
我试图解决您的情况,请看一下
我遍历了rfid
数据,找到了before
和after
time
和weights
。比较值并根据决策将msg
和rfid
推到result
数组
var acquiredData = {
weigthLitter: [
{ time: 1561458881182, value: 2130 },
{ time: 1561458881584, value: 1000 },
{ time: 1561458882789, value: 2130 },
{ time: 1561458884599, value: 3290 }],
rfid: [ { time: 1561458881523, value: '1000 0000 0000 0001' },
{ time: 1561458882212, value: '1000 0000 0000 0001' },
{ time: 1561458883941, value: '1000 0000 0000 0002' } ]
}
var { weigthLitter, rfid } = acquiredData
var result = rfid.reduce((acc, el, id)=> {
var currentRfid = el.value;
var after = weigthLitter.find(w => w.time > el.time);
var before = weigthLitter.find(w => w.time < el.time);
if(rfid[id-1] === undefined || (rfid[id-1] && rfid[id].value === rfid[id-1].value)){ // checking rfid is same or undefined
if (before.value > after.value) {
acc.push({rfid: el.value, msg: "animal got out of the box" })
} else {
acc.push({rfid: el.value, msg: "animal came back in" })
}
} else {
acc.push({rfid: el.value, msg: "another animal came back in" })
}
return acc
}, [])
console.log(result) //[{"rfid":"1000 0000 0000 0001","msg":"animal got out of the box"},{"rfid":"1000 0000 0000 0001","msg":"animal came back in"},{"rfid":"1000 0000 0000 0002","msg":"another animal came back in"}]
答案 1 :(得分:0)
好吧,我终于设法使用索引来实现这一目标(我以前没有考虑过,但是当您专注于自己的想法有时很难再换个角度时,我感到有点愚蠢)< / p>
for (var i = 0; i < this.acquiredData.rfid.length; i++)
{
var weigths = this.acquiredData.weigthLitter;
var currentRfid = this.acquiredData.rfid[i];
var indexSup = weigths.findIndex(w => w.time >= currentRfid.time);
Log("RFID : " + currentRfid.value + "@" + currentRfid.time + " Before :" + weigths[indexSup - 1].value+ "@" + weigths[indexSup - 1].time + " After :" + weigths[indexSup].value + "@" + weigths[indexSup].time);
var evttype = weigths[indexSup - 1].value > weigths[indexSup].value ? 'exit' : 'entrance';
toSend.events.push({
type : evttype,
bunnyId : currentRfid.value.toString(),
value : currentRfid.time
});
}
在剩下的模拟过程中,我设法确保当我在代码的这一点上时,数据将是完整的。