Trying to make a function to find nearby items, enemies, friends

let finderNear = () => {
const friend = hero.findNearest(hero.findFriends());
const enemy = hero.findNearest(hero.findEnemies());
const item = hero.findNearest(hero.findItems());
if(friend){return friend;}
if(enemy){return enemy;}
if(item){return item;}
};

I’m trying to make a function that will find nearby stuff.

is there a way to call this function and assign different return statements to different variables.

for example.

while(true) {
    let nearStuff = finderNear();
    if(nearStuff){
        hero.moveXY(x.pos.x, x.pos.y);
        }
}

when done like this, the function finds everything, but I would like to assign items, enemies and friends found to different variables

When I use that If statement ( if(nearStuff) ), it moves to everything.

is there a way to declare an if statement that will only focus on items, enemies, or friends?

I can separate this into 3 functions and solve what I’m trying to achieve but I’m wondering if there is a way to combine everything into one function and use what I need, depending on the level.

I hope that makes sense

or something like this. On this example I have nested each function inside one

let finderNear = () => {
    let nearFriend = () => {
        const friend = hero.findNearest(hero.findFriends());
        if (friend) {
            return friend;
        }
    };
    let nearEnemy = () => {
        const enemy = hero.findNearest(hero.findEnemies());
        if (enemy) {
            return enemy;
        }
    };
    let nearItem = () => {
        const item = hero.findNearest(hero.findItems());
        if (item) {
            return item;
        }
    };
};

how can I assign nearItem function to a variable that is outside of this function?

for example, I would like to use nearItem function here

while(true) {
    let near = finderNear();
// I want to be able to use nearItem here but I cannot access it 
    if(nearItem){
        hero.moveXY(x.pos.x, x.pos.y);
        }
}