0

I'm getting data from an object from Facebook, and would like to select all objects in an array, that has a child value of xxx.

The object (simplified of course) is structured like this:

var friends = [
    {
        id: 123456,
        name: "Friend1 name",
        education: [
            {
                school: {
                    name: "School1 name"
                },
                type: "Highscool"
            }
        ]
    },
    {
        id: 3456789,
        name: "Friend2 name",
        education: [
            {
                school: {
                    name: "School2 name"
                },
                type: "College"
            }
        ]
    }
]

Let's assume that I want to get all objects with education.type = "Highscool". How can I do this, without looping though the entire object...?

4
  • I'm not sure this is possible without some sort of loop Commented Mar 13, 2013 at 9:12
  • I was affraid of that... :-/ Commented Mar 13, 2013 at 9:12
  • And how many elements can education have? Should we check only the first one education[0]? Commented Mar 13, 2013 at 9:18
  • I assume "Highscool" is a typo. :-) I used "Highschool" in my answer, but adjust as necessary. Commented Mar 13, 2013 at 9:21

1 Answer 1

1

How can I do this, without looping though the entire object...?

You can't. But it doesn't have to be hard:

var highSchoolFriends = friends.filter(function(friend) {
    var keep = false;
    friend.education.some(function(entry) {
        if (entry.type === "Highschool") {
            keep = true;
            return true;
        }
    });
    return keep;
});

That uses the ES5 Array#filter and Array#some functions. filter returns a new array made up of the entries in the friends array for which the iterator function returns true. some loops through an array until the iteration function you give it returns true (I used it instead of Array#forEach because you can stop it early). If you need to support older browsers that don't have those yet, they're ones of the ones an "ES5 shim" can give you.

Or you just do the simple loops:

var i, j;
var highSchoolFriends = [];
var friend;

for (i = 0; i < friends.length; ++i) {
    friend = friends[i];
    for (j = 0; j < friend.education.length; ++j) {
        if (friend.education[j].type === "Highschool") {
            highSchoolFriends.push(friend);
            break;
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply. It's not hard, but my friend list is huge, and the app is for mobiles, so I hoped not to do alot loops. But I guess there is no other way... Thanks for your time.
@KennethB: No worries, hope that helped.

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.