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.