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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:00:10+00:00 2026-05-31T13:00:10+00:00

I have tried many things to get this working correctly but I can’t Here

  • 0

I have tried many things to get this working correctly but I can’t

Here is my problem:

I have an array of objects in javascript which looks something like this:

var myArrOfObjs = [];
var myObject1 = {};
myObject1.key = '1234';
myObject1.label = 'Richard Clifford';

myArrOfObjs.push( myObject1 );

and I need to do something like:

if( !containsObject( myObject1, myArrOfOjbs ) ){
    // Do stuff
}

I need the containsObject function to check for key values within the found object (if any), so if containsObject( myObject1, myArrOfOjbs ) finds the object, I need to check to see if the key of that is the same as the one I am currently trying to push.

The reason I need it to check the keys is because I have tried this function which I found else where on StackOverflow, but it isn’t quite working.

function containsObject(obj, list) {
    var i;
    for (i = 0; i < list.length; i++) {
        if (list[i] == obj) {
            return true;
        }
    }

    return false;
}

It still pushes the object to the array even when it already contains the object.

Please let me know if you need anything clearing up, I realise that it isn’t the easiest post to read/understand.

Thanks!

  • 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-31T13:00:12+00:00Added an answer on May 31, 2026 at 1:00 pm

    You need to change the equality test to compare the keys:

    function containsObject(obj, list) {
        var i;
        for (i = 0; i < list.length; i++) {
            if (list[i].key === obj.key) {
                return true;
            }
        }
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried many things, but I'm still not getting this to work. Here's
I have tried so many things, cannot figure this out, I am using this
This should be really simple, but I have tried many solutions posted on the
I have tried what seems like everything - I've done similiar things many times
I want to get the page rank for any website. I have tried many
I'm trying to add values from array to DB, have tried many variuos examples
I have downloaded many vim color schemas and tried them out, but many of
Have tried to find solutions for this and can't really come up with anything.
I have tried searching for an answer to this but my searches have come
This may be a very dumb question but I can't seem to get it

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.