Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:23+00:00 2026-05-22T15:57:23+00:00

I am trying to find the index of an object in an array in

  • 0

I am trying to find the index of an object in an array in jquery.
I cannot use jQuery.inArray because i want to match objects on a certain property.
I am using:

jQuery.inObjectArray = function(arr, func)
    {
        for(var i=0;i<arr.length;i++)
            if(func(arr[i]))
                return i;
        return -1;
    }

and then calling:

jQuery.inObjectArray([{Foo:"Bar"}], function(item){return item.Foo == "Bar"})

Is there a built in way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:57:24+00:00Added an answer on May 22, 2026 at 3:57 pm

    Not sure why each() doesn’t work for you:

    BROKEN — SEE FIX BELOW

    function check(arr, closure)
    {
        $.each(arr,function(idx, val){
           // Note, two options are presented below.  You only need one.
           // Return idx instead of val (in either case) if you want the index
           // instead of the value.
    
           // option 1.  Just check it inline.
           if (val['Foo'] == 'Bar') return val;
    
           // option 2.  Run the closure:
           if (closure(val)) return val;
        });
        return -1;
    }
    

    Additional example for Op comments.

    Array.prototype.UContains = function(closure)
    {
        var i, pLen = this.length;
        for (i = 0; i < pLen; i++)
        {
           if (closure(this[i])) { return i; } 
        }
        return -1;
    }
    // usage:
    // var closure = function(itm) { return itm.Foo == 'bar'; };
    // var index = [{'Foo':'Bar'}].UContains(closure);
    

    Ok, my first example IS HORKED. Pointed out to me after some 6 months and multiple upvotes. : )

    Properly, check() should look like this:

    function check(arr, closure)
    {
        var retVal = false; // Set up return value.
        $.each(arr,function(idx, val){
           // Note, two options are presented below.  You only need one.
           // Return idx instead of val (in either case) if you want the index
           // instead of the value.
    
           // option 1.  Just check it inline.
           if (val['Foo'] == 'Bar') retVal = true; // Override parent scoped return value.
    
           // option 2.  Run the closure:
           if (closure(val)) retVal = true;
        });
        return retVal;
    }
    

    The reason here is pretty simple… the scoping of the return was just wrong.

    At least the prototype object version (the one I actually checked) worked.

    Thanks Crashalot. My bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to find index of object in a JavaScript array with jQuery
I am trying to find a clean way of referencing an array's index using
I have a NSMutable Array and was trying to find the index number of
I'm trying to find out the index number of the last list item but
searching around trying to find a way to add index on suffix of a
I'm trying to find out a way to know the index of a anchor
I'm using Lucene, and I'm trying to find a way to index and retrieve
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
I'm trying to find an efficient way to find the rank of an object
I'm trying to get the index of the trigger element in the array. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.