0

This is basically something that makes zero logical sense, and I'm not sure why this is happening.

When you create a function to compare attribute values of an array of objects (essentially JSON object), it refuses to find the index. However, OUTSIDE the function, it seems to work perfectly fine.

However, the problem is

var peoples = [
  { "name": 44, "dinner": "pizza" },
  { "name": 65, "dinner": "sushi" },
  { "name": 33, "dinner": "hummus" }
];

var val = 33;
$("#t").append(get_index_of_array_based_on_value(peoples, val));

function get_index_of_array_based_on_value(array, val) {
    $.each(array, function (index, obj) {
        $.each(obj, function (attr, value) {
            console.log(" attr: " + attr + " == " + value + " (" + val + ") {{" + index + "}} ");
            if (value == val) {
                return index;
            }
        });
    });
}

http://jsfiddle.net/QStkd/2327/

The above does not work.

The below script does work.

http://jsfiddle.net/QStkd/2330/

The below script is simply the same script except outside the function. When you put stuff into functions it suddenly refuses to find the index based on the value.

0

2 Answers 2

3

You cannot return a value from a $.each call. You are inside a callback, your return doesn't affect the main function.

When you use return inside the $.each callback, it's similar to break/continue in a for/while loop. A falsy value will break the loop, and a truthy value is like calling continue.

You need to return from the main function, get_index_of_array_based_on_value, not from the $.each.

function get_index_of_array_based_on_value(array, val) {
    var returnVal = null;

    $.each(array, function (index, obj) {
        $.each(obj, function (attr, value) {
            console.log(" attr: " + attr + " == " + value + " (" + val + ") {{" + index + "}} ");
            if (value == val) {
                returnVal = index;
                return false; // break;
            }
        });

        if(returnVal !== null){
            return false;  // break the outer loop
        }
    });

    return returnVal;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Nicely answered Rocket - you just beat me to it.
I'm like a ninja when it comes to answers. Especially when it's problems like this one that I've dealt with in my code.
Thank you, you guys are real smart. Two skilled developers couldn't notice that problem, probably needed to sleep on it. I guess because in other languages, a return is a return and you don't usually have functions inside functions unless its Python.
JavaScript (and jQuery) loves callback functions :-D
1

thy this

var peoples = [
  { "name": 44, "dinner": "pizza" },
  { "name": 65, "dinner": "sushi" },
  { "name": 33, "dinner": "hummus" }
];

var val = 33;

$("#t").append(get_index_of_array_based_on_value(peoples, val));

function get_index_of_array_based_on_value(array, val) {
  for(var index = 0; index < array.length; index++){
    for(var attr in array[index]) {
        var value = array[index][attr];
        console.log(" attr: " + attr + " == " + value + " (" + val + ") {{" + index     + "}} ");
         if (value == val) {
            return index;
        }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.