How to execute hierarchy

I would like to repeat the found structures, but I do not know which method is the best for this.

I tried this one:

for (var ext in creep.room.find(FIND_MY_STRUCTURES, {filter: { structureType: STRUCTURE_EXTENSION }})){
    console.log(ext.energy);
}

but that will not work.

So now I use an approach that works, but looks ugly:

for(var i = 0; i < creep.room.find(FIND_MY_STRUCTURES, {filter: { structureType: STRUCTURE_EXTENSION }}).length; i++) {
    var ext = creep.room.find(FIND_MY_STRUCTURES, {filter: { structureType: STRUCTURE_EXTENSION }})[i];
    console.log(ext.energy);
}

I'm not sure, maybe this is a js related question. I am completely new to js. Can you give some advice about this?

+4
source share
2 answers

ext contains the key, not the value of the result.

So what I did is move the results from the loop and put the variable with the name results. So I have a variable for reference in a loop.

, ext , . , - "key".energy undefined, String .

, , :

var results = creep.room.find(FIND_MY_STRUCTURES, {filter: { structureType: STRUCTURE_EXTENSION }});
for (var ext in results){
    console.log(results[ext].energy);
}
+2

, Array.forEach !

creep.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_EXTENSION } })
    .forEach((ext) => { console.log(ext.energy); });
0

All Articles