因此,我正在使用一个对象列表在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
EDIT: 这个数据是加入到另一个数据源中的,下面是基于vehicle_id加入的函数。
将List加入到另一个数据源。
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}`)