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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:32:58+00:00 2026-05-15T13:32:58+00:00

I need to find which id numbers are missing inside s.data compared to users.

  • 0

I need to find which id numbers are missing inside s.data compared to users.
Is there a better(smaller code) way to compare?

Thanks 😉

if(users.length != undefined)
{

    for(y=0;y<users.length;y++) 
    {
        var left = true;
        for(y2=0;y2<s.data.length;y2++) 
        {
            if(users[y].client_id==s.data[y2].client_id) {left = false;break;}
        }
        if(left) {users[y].ref.remove();delete users[y];}

    }

}
else if(!jQuery.isEmptyObject(users))
{
    var left = true;
    for(y2=0;y2<s.data.length;y2++) 
    {
        if(users.client_id==s.data[y2].client_id) {left = false;break;}
    }
    if(left) {users.ref.remove();users = {};}
}

Haven’t checked if this is working code. 🙂

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

    First, off, the 2nd branch appears to be nothing but a specialization of the first branch. You can use this to either make the “2nd” users = [users] (in which case users really means users and not a-user) and eliminates the top branch entirely, or remove the the logic into a function invoked per-user.

    Now, to tackle the inner loop: What this is a ‘map’ and a ‘contains’. Looking at it just in terms of a contains:

    // Returns true if any item in data.client_id (an array)
    // is that of user.client_id
    function dataContains (user, data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].client_id == user.client_id) {
          return true
        }
      }
      return false
    }
    

    Now the code is reduced to:

    for (each user) {
      if (!dataContains(user, data)) {
        // do something here
      }
    }
    

    However, we could go one step further and use a generic ‘contains’ if we also have a ‘map’. The final form is then:

    var dataIds = map(data, function (x) { return x.client_id })
    for (each user) {
      if (!contains(user.client_id, dataIds)) {
        ..
      }
    }
    

    Where the ‘contains’ is much more generalized:

    // Returns true iff item is contained within arr
    function contains (item, arr) {
      // Just do what the comment documentation says
    }
    

    If you are using jQuery you already have handy functions:
    ‘contains’ – inArray, and a “sorta” ‘map’ – map. However, be warned! The jQuery ‘map’ is really a flat-map and was given an incorrect name and incomplete documentation!

    I believe ECMAScript ED5 has these functions standard.

    Also, you could invert the client_id’s in the data to object keys and simply test for key existence, which is O(1) vs. O(n) iff the look-up is built once (or at least much, much less than it’s used) and so it may be “theoretically” better. The size of n makes a large difference if it will actually matter, if at all. In this case it’s likely the look-up could be built incrementally and saved between times this code is executed.

    var existingIds = {}
    for (var i = 0; i < data.length; i++) {
      existingIds[data[i].client_id] = true
    }
    for (each user) {
      if (!existingIds[user.client_id]) {
        ..
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 480k
  • Answers 480k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your curly braces are OK, but you're missing a few… May 16, 2026 at 6:14 am
  • Editorial Team
    Editorial Team added an answer OK, I've figured it out... It turns out that "member"… May 16, 2026 at 6:14 am
  • Editorial Team
    Editorial Team added an answer Strange - RequiresNew in the inner (Logging) TransactionScope should work.… May 16, 2026 at 6:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.