JessycaFrederick
JessycaFrederick

Reputation: 408

Javascript returning "undefined" when it knows what the string is

I've created a function that loops through one array, using each value (item) to check whether a different value (clicked_item) exists in an array called item.

This works great. In the log statement, it correctly identifies the parent (item) and the type (string). Output in console: FOUND PARENT FOR: pinot-noir-gifts IT IS ITEM: red_wine_gifts TYPE: string

However return item returns undefined when it should return red_wine_gifts. The result is that clicked_item_parent is undefined.

Why would the script be able to log a value but the function returns it as undefined?

function find_parent(clicked_item)
{
    filter_have_children.forEach( function(item){
        var look_here = filter_children[item];
        if ( look_here.indexOf(clicked_item) != "-1" ) 
        {
            console.log("FOUND PARENT FOR: "+clicked_item+" IT IS ITEM: "+item+" TYPE: "+typeof item);
            return item;
        }
    });
}

var clicked_item_parent = find_parent(clicked_item);

if ( typeof clicked_item_parent !== "undefined" )
{
    // Do some things
} 
else console.log("parent was undefined");

Upvotes: 3

Views: 1421

Answers (2)

Yousaf
Yousaf

Reputation: 29282

forEach doesn't returns any value.

Returning item from forEach won't make it the return value of the find_parent function. You need to save the value you want to return in a local variable and return that local variable from find_parent function.

function find_parent(clicked_item)
{
   let val;   // variable to hold the item that should be returned

    filter_have_children.forEach( function(item){
        var look_here = filter_children[item];
        if ( look_here.indexOf(clicked_item) != "-1" ) 
        {
            console.log("FOUND PARENT FOR: "+clicked_item+" IT IS ITEM: "+item+" TYPE: "+typeof item);
            val = item;     // save item in local variable
        }
    });

   return val;     // return the local variable containing the item
}

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074475

Your return is returning a value from the forEach callback (that return value is ignored). Your find_parent function doesn't ever return anything, which is why calling it results in undefined.

The find method is what you'd want in this case:

function find_parent(clicked_item)
{
    return filter_have_children.find( function(item){
//  ^^^^^^                      ^^^^
        var look_here = filter_children[item];
        return look_here.indexOf(clicked_item) != -1; // <= Note -1 should be a number, not a string.
    });
}

Your find callback should return a truthy value for the item you want to find, or a falsy value for any other item. It gets called for each element in the array in order, stopping the first time your callback returns a truthy value. find's result is the item that your callback said was the right one, or undefined if your callback never returns a truthy value.

Upvotes: 2

Related Questions