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 7813367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:50:07+00:00 2026-06-02T04:50:07+00:00

In javascript I need to know if array contains value. Values are objects and

  • 0

In javascript I need to know if array contains value. Values are objects and I can have different instances of same object, which means $.inArray(…) will not work. I know how to do my task using $.each(…) and my question is – is it possible to pass function with value’s comparing logic to any of jQuery methods (see sample with desired sintax)?

    // values
var val1 = { id: 1, description: 'First value'};
var val2 = { id: 2, description: 'Second value'};
var val3 = { id: 3, description: 'Third value'};        
// array of values
var values = [ val1, val2, val3 ];
// inArray of jQuery to know if value is in array -> returns TRUE
var isInArray = $.inArray(val2, values) > -1;

// another instance of same object "val2"
var val2_anotherInstance = { id: 2, description: 'Second value'};
// inArray works as expected -> returns FALSE but desirable result is TRUE
var isInArray_anotherInstance = $.inArray(val2_anotherInstance, values) > -1;

// define function for comparing values (any logic can be implemented, for example searching by Id)
var valueComparer = function(first, second) {
    return first.id == second.id && first.description == second.description;
}
// function compares objects and returns true for different instances
alert(valueComparer(val2, val2_anotherInstance));

// desirable sintax:
// is something like this possible ???      
// NOTE next line not correct
isInArray_anotherInstance = $.inArray(val2_anotherInstance, values, valueComparer) > -1;
// actually what I want is specify criteria for searching value in array
  • 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-06-02T04:50:13+00:00Added an answer on June 2, 2026 at 4:50 am

    Try grep:

    var val1 = { id: 1, description: 'First value'};
    var val2 = { id: 2, description: 'Second value'};
    var val3 = { id: 3, description: 'Third value'};        
    
    var values = [ val1, val2, val3 ];
    
    // another instance of same object "val2"
    var val2_anotherInstance = { id: 2, description: 'Second value'};
    
    
    var items = $.grep(values, function(x) {
        return x.id == val2_anotherInstance.id
    })
    
    var found = items.length > 0
    

    For more elegance, you can use a boolean aggregator function, as provided in this answer:

    val2_in_array = $.some(values, function() {
        return this.id == val2_anotherInstance.id
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an Array-derived class in JavaScript and need to know which functions
I have a created 4 dimensional array in JavaScript. One column contains String values
I have a numeric javascript array, that contains several objects with geodata in it.
I need to have some information about the scoping in JavaScript. I know that
I have a JavaScript array of objects like this: box[0] = {...} box[1] =
I have a collection of objects in JavaScript like this: Object collection = new
does anybody know how to call C++ functions or methods via JavaScript. Need scripting
I need a javascript bookmark to take the url I have in the clipboard
I have a jpg image. I need to know overall average the color of
I need to be able to store objects in javascript, and access them very

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.