0

My jQuery function is acting funny and I can't for the life of me figure out why?

function getValueFromSettingArray(array,key){
    if(array && array.length > 0){
        console.log("Setting Array Greater Than 0",array);
        $.each(array, function(index,item){
            console.log(index,item);
            if(item.name == key){
                console.log("FOUND",item.value);
                return item.value;
            }
        });
    }

    return false;
}

The console sends "FOUND" but the return value is "false", which I have confirmed is coming from the ending return false; The console outputs are even in proper order.

Doing the same thing in a for loop provides the expected results

function getValueFromSettingArray(array,key){
    if(array && array.length > 0){
        console.log("Setting Array Greater Than 0",array);
        for(let i=0; i < array.length; i++){
            let item = array[i];
            console.log("item loop " + i, item);
            if(item.name == key){
                console.log("FOUND",item.value);
                return item.value;
            }
        }
    }

    return false;
}

Everything I am reading online says $.each() is Synchronous, but it's acting like it's Asynchronous in this manner. I am at a loss, it could be the gaining hours of straight coding, what I am doing wrong? Any suggestions?

Edit: The question was closed as duplicate, but I am not asking how to get the values from an array. I am asking why my result isn't as expected.

@ivor kindly commented pointing out that my inner 'return item.value' was returning the function of my $.each(). This has provided clarity to my issue.

jjonesdesign
  • 410
  • 2
  • 14
  • 2
    `return` applies to the enclosing function. The enclosing function in your `$.each` loop is the `function(index,item){ ... }`. Not your `getValueFromSettingArray` function. – Ivar Mar 24 '23 at 23:03
  • 2
    FYI you don't need to use jQuery for this. JavaScript has `Array.prototype.find()` that will return the element that matches a condition. – Barmar Mar 24 '23 at 23:09
  • @Ivar This makes a lot of sense! Thanks for pointing that out. What would be the proper way to send a result in this context? – jjonesdesign Mar 24 '23 at 23:40
  • @Barmar As far as I am aware, this is for single dimension arrays. I am working with a multi-dimension array. – jjonesdesign Mar 24 '23 at 23:40
  • 1
    I don't see anything in your code that implies a multimensional array. It looks like an array of objects. – Barmar Mar 24 '23 at 23:44
  • 1
    `array` is a 1-d array, `item` is an object. – Barmar Mar 24 '23 at 23:44
  • @Barmar My apologies, I meant to say key value pairs. – jjonesdesign Mar 24 '23 at 23:46
  • 1
    That's what an object is. The question I linked to shows how to search for a particular value in an array of objects. `return array.find(item => item.name == key)` – Barmar Mar 24 '23 at 23:48
  • @Barmar Thank you for clarifying. I wasn't aware that I could do that! That's very helpful thank you <3 – jjonesdesign Mar 24 '23 at 23:53

0 Answers0