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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:46:55+00:00 2026-06-03T18:46:55+00:00

I need to work through a source array of objects, many of the objects

  • 0

I need to work through a source array of objects, many of the objects in the array have three property values which will be the same. It is these values that will be used to create a new object and push it on to destination array. When another object on the source array comes up with the same three property values as one of the objects on the destination array the matching object on the destination array will have its visit count incremented by one.

To help you understand, in the source array each object is a record of a meal that belongs to a user. In the second array I need to store the user details and the number of their meals.

I’ve tried a few solutions which have failed like the one below. I thought that the code below would create a literal object, check if it is in the destination array by finding it’s indexOf (-1 for not found) and if it’s not found push it on. The problem is that it never finds the objects, if I search through 3000 meals the second array ends up 3000 long!

The code below does not try to store the visit count.

userArray = new Array();
for (var i = 0; i < filteredObjects.length; i++) {
    var user = { 
        forname: filteredObjects[i].forname,
        surname: filteredObjects[i].surname,
        dincat: filteredObjects[i].dincat,
    };
    var index = userArray.indexOf(user);
    if (index = -1) {
        userArray.push(user);
    }
}
  • 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-03T18:47:01+00:00Added an answer on June 3, 2026 at 6:47 pm

    This doesn’t work because the user object that you create in the loop is not the same as any of the objects you added inside userArray. They might contain the same keys and values, but strictly speaking (===) they’re not the same.

    To help your code, you can add a user map object:

    var userArray = new Array(),
    userMap = {};
    
    for (var i = 0, item; item = filteredObjects[i]; ++i) {
        var userKey = item.forname + '-' + item.surname + '-' + item.dincat;
    
        if (!(userKey in userMap)) {
            userArray.push({
                forname: filteredObjects[i].forname,
                surname: filteredObjects[i].surname,
                dincat: filteredObjects[i].dincat,
            });
            userMap[userKey] = true;
        }
    }
    

    The user map is an object that uses its keys to determine whether you have already inserted a user before. This works by choosing a user identifier, in your case the combination of first name, surname and dincat.

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

Sidebar

Related Questions

using this tutorial http://railscasts.com/episodes/57-create-model-through-text-field need to make it work in my app, was on
I need to work on C++ project on my windows machine. My project will
I need to work around a Java bug in JDK 1.5 which was fixed
I am just trying to think through a process I need where I have:
I work on an open-source Java project, and we have a lot of resource
I have run into this issue too many times and need this to be
I can't work out from looking through the source what the difference is between
I am trying to write a script that will walk through an HTML source
I need to work with some old C++ code that was developed in Visual
I need to work with a third-party Java library from .NET. Can anyone recommend

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.