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

  • Home
  • SEARCH
  • 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 7161651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:36:09+00:00 2026-05-28T13:36:09+00:00

I need to push a hash into array only if it doesn’t contain the

  • 0

I need to push a hash into array only if it doesn’t contain the hash that i want to add.
For example i have an array of hashes :

var someArray = [
        {field_1 : "someValue_1", field_2 : "someValue_2"},
        {field_1 : "someValue_3", field_2 : "someValue_4"},
        {field_1 : "someValue_5", field_2 : "someValue_6"}
    ]

The value

{field_1 : "someValue_1", field_2 : "someValue_2"}

should not be pushed into array as it is already there, but the value

{field_1 : "someValue_7", field_2 : "someValue_8"}

should, as the array does’t contain such value.

Is there any way to do it using jQuery?

Now i’m just using $.each loop and check if the array contains some hash. If it does i trigger a flag. But i don’t like this solution.

  • 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-28T13:36:10+00:00Added an answer on May 28, 2026 at 1:36 pm
    1. create custom prototype to check the presence of object in the array. Please check the prototype design below.
    Array.prototype.contains=function(x){
     for(i in this){
       if((x.field_1==this[i].field_1)&&(x.field_2==this[i].field_2))
         return true;  
     }
     return false;
    }
    
    1. Using above prototype, you can check if object present in an array.
     var someArray = [
            {field_1 : "someValue_1", field_2 : "someValue_2"},
            {field_1 : "someValue_3", field_2 : "someValue_4"},
            {field_1 : "someValue_5", field_2 : "someValue_6"}
        ];
    
    var a1={field_1 : "someValue_1", field_2 : "someValue_2"};
    var a2={field_1 : "someValue_7", field_2 : "someValue_8"};
    
    if(!someArray.contains(a1))   someArray.push(obj);  // it will NOT BE pushed
    if(!someArray.contains(a2))   someArray.push(obj); // it will be pushed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just heard that - push notification is possible in iPhone I need
I need to push elements from one array into respective rows of another array.
i need to push data in javascript array into database using PHP first i
I have a need to push out a file package that includes a driver
I need a little push in the right direction. Here's my problem: I have
Need to an expression that returns only things with an I followed by either
i have a problem where I need to have an array as a value
I have a hash of arrays, and I need to sort it first on
Hey everyone, I have a database result that returns from a method. I need
I'm going to need to push and pull files from a SharePoint site that

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.