【问题标题】:Nested filter doesn't work嵌套过滤器不起作用
【发布时间】:2016-07-20 21:50:53
【问题描述】:
// Build structures
var structs = curRoom.find(FIND_CONSTRUCTION_SITES, {
    filter: (structure) => {
        return
            _.filter(Memory.jobs.worker.normal,
                (job) => {
                    return job.id == structure.id;
                }).length == 0 // Test if this structure is already in the queue
            && _.filter(Game.creeps,
                (creep) => {
                    return creep.memory.curJob != undefined && creep.memory.curJob.id == structure.id;
                }).length == 0; // Test if a creep is already working on this structure
    }
});

上面的代码返回 0 个建筑工地(应该通过测试的 40 个建筑工地),但每个建筑工地都通过了(外部)过滤器。

我还测试了内部过滤器(使用.length == 0):

console.log(<filter1>); // true

console.log(<filter2>); // true

console.log(<filter1>, <filter2>) // true true

console.log(<filter1> && <filter2>

是我遗漏了什么还是我做错了什么?

【问题讨论】:

  • 奇怪。对我来说,代码看起来很可靠。但是,是的,一般来说,将内部过滤器移到外部过滤器之前是一个更好的主意。

标签: screeps


【解决方案1】:

我通过在外部过滤器之前(和外部)执行内部过滤器解决了这个问题。这也节省了一些时间,因为过滤器基本相同,不必多次执行(用于修复等其他操作)

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2018-06-08
    相关资源
    最近更新 更多