因此,我正在使用一个对象列表来构建JavaScript中的值数组,但出于某种原因,它正在构建一个包含值的键列表。看来我做得对。
原始数据:
"features" : [
{
"attributes" : {
"vehicle_id" : 2077,
"cleaning_time" : 1588198260000
},
"geometry" :
{
"x" : 0,
"y" : 0
}
},
{
"attributes" : {
"vehicle_id" : 2071,
"cleaning_time" : 1588258620000
},
"geometry" :
{
"x" : 0,
"y" : 0
}
},
构建阵列:
let list = [];
for(let item in features){
let date = new Date(features[item].attributes.cleaning_time)
if((new Date() - date) < 7200000){
console.log(features[item].attributes.vehicle_id);
let veh = features[item].attributes.vehicle_id
list.push(veh);
}
}
最终阵列:
[]
0: 6618
1: 2204
2: 2204
3: 6618
4: 2204
5: 2204
length: 6
<prototype>: Array []
app.js:176:11
编辑:该数据正在加入另一个数据源。下面是用于基于vehicle_id加入的函数。
加入列表到另一个数据源:
for(let i in data){
let veh = data[i].vehicle.vehicle.id;
data[i].vehicle.isClean = list.includes(veh);
}
此脚本旨在筛选保存时间小于2小时的数据。此脚本每15秒运行一次,每次都会重新评估数据。
据我所知,您的代码运行正常。根据一条评论,我调整了代码。
let list = [];
var features = [
{
"attributes" : {
"vehicle_id" : 2077,
"cleaning_time" : new Date() - 100
},
"geometry" :
{
"x" : 0,
"y" : 0
}
},
{
"attributes" : {
"vehicle_id" : 2071,
"cleaning_time" : new Date() - 100
},
"geometry" :
{
"x" : 0,
"y" : 0
}
}
]
for(let item in features){
console.log(item)
let date = new Date(features[item].attributes.cleaning_time)
console.log(`(new Date() - date) < 7200000 is ${(new Date() - date) < 7200000}`)
if((new Date() - date) < 7200000){
console.log(features[item].attributes.vehicle_id);
let veh = features[item].attributes.vehicle_id
list.push(veh)
}
}
console.log(`list[0] == undefined is ${list[0] == undefined}`)