gpt4 book ai didi

javascript - 过滤驾驶员 ID

转载 作者:行者123 更新时间:2023-12-01 02:32:22 25 4
gpt4 key购买 nike

我想要的是,如果驱动程序位于 BusyAgent 中,那么 freedriver 的最终输出应包含来自 fetchAgent 的所有输入,除了busyAgent 中的那些。

预期输出[{"tag":"","array":[{"device_type":0,"fleet_id":23691,"eta":0}]}]

var fetchAgent = [{
"tag": "",
"array": [{
"device_type": 0,
"fleet_id": 24117,
"eta": 0
}]
}, {
"tag": "",
"array": [{
"device_type": 0,
"fleet_id": 23691,
"eta": 0
}]
}];


var busyAgent = ['24102', '24103', '24117'];

//Cannot read property 'filter' of undefined

if (Array.isArray(busyAgent) && busyAgent.length > 0) {
freeDrivers = fetchAgent.map(e => {
e.array = e.array.filter(a => busyAgent.indexOf("" + a.fleet_id))
return e;
});
} else {
freeDrivers = fetchAgent;
}
console.log(JSON.stringify(freeDrivers));

最佳答案

反转过滤器的大小写,也许也将 map 替换为 filter ,这样就不会出现空值。否则,您将不得不进行另一次过滤。

var fetchAgent = [{
"tag": "",
"array": [{
"device_type": 0,
"fleet_id": 24117,
"eta": 0
}]
}, {
"tag": "",
"array": [{
"device_type": 0,
"fleet_id": 23691,
"eta": 0
}]
}];


var busyAgent = ['24102', '24103', '24117'];
if (Array.isArray(busyAgent) && busyAgent.length > 0) {
freeDrivers = fetchAgent.filter(e => {
e.array = e.array.filter(a => busyAgent.indexOf("" + a.fleet_id) < 0);
if( e.array.length > 0) return e;
});
} else {
freeDrivers = fetchAgent;
}

console.log(JSON.stringify(freeDrivers));

关于javascript - 过滤驾驶员 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com