How can I return a value from a recursive function?

Hello, I'm trying to return a value from the following recursive function:

function recurse(data, found) {
    if (found) {
        return true;
    }

    let iPhones = [
        "06:0f:a4:98:59:42", //T
        "8a:71:de:d1:69:b9", //A
        "8a:e8:09:93:ea:73" //F
    ]

    data.forEach(function (element){
        if(Array.isArray(element)) {
            recurse(element, found);
        } else {
            if (element.hasOwnProperty('mac')) {
                if (iPhones.includes(element['mac'])) {
                    node.warn(element['name']+ ': ' + element['uptime']);
                    found = true;
                    return true;
                }
            }
        }
    });
}

var found = false;
var present = false;
present = recurse(msg.payload, found);

node.warn (present);

However, the node.warn line returns undefined.

Suggestions?

Thanks
Tom

Sorted using context.set("found",true) :grinning:

You call the recursive() but do not set it to found.

function recurse(data, found) {
    let iPhones = [
        "06:0f:a4:98:59:42", //T
        "8a:71:de:d1:69:b9", //A
        "8a:e8:09:93:ea:73" //F
    ]

    data.forEach(function (element){
        if(Array.isArray(element)) {
           found = recurse(element, found);   
        } else {
            if (element.hasOwnProperty('mac') && iPhones.includes(element['mac'])) {
                node.warn(element['name']+ ': ' + element['upTime']);
                found = true;
                return found;
            }
        }
    });
    return found;
}

var found = false;
var present = false;
present = recurse(msg.payload, found);

node.warn (present);

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.