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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:10:45+00:00 2026-06-01T04:10:45+00:00

I have two arrays. If a user adds a product, we put it in

  • 0

I have two arrays. If a user adds a product, we put it in the ProductArray. If they remove that product, we add it to the ProductArrayRemove array as well as remove it from the product array. (We need to know products that have been added as well as products that have been removed. This requires redundancy.)

ProductArray = JSON.parse(ProductArray);
ProductArrayRemove = JSON.parse(ProductArrayRemove);

When I add and item to the array, I simply do this:

 ProductArray.push(ItemID);
 ProductArrayRemove.push(ItemID);

But when I remove it, I have to do this:

var len = ProductArray.length;
for (i = 0; i < len; i++) {
    ProductID = ProductArray[i];
    if (ProductID == ItemID) {
        ProductArray.splice(i,1);
        break;
    }
}

It seems that there should be a better way of accomplishing this task.

Is there a more efficient means of getting rid of a single item (that will always be an integer) from an 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-01T04:10:46+00:00Added an answer on June 1, 2026 at 4:10 am

    One different idea that would be super fast to remove an item is to just maintain a single list of items and have a property on each item for whether it is removed or not.

    Then to remove an item, all you do it set it’s property obj.removed = true.

    The add it back again, you just change the value of that property.

    To iterate just the added items, you just skip the ones with the .removed == true property. To iterate just the removed items, you do just the reverse. Here would be a couple of iterators:

    ProductArray.iterateAdded = function(fn) {
        for (var i = 0; i < this.length; i++) {
            if (!this[i].removed) {
                if (fn.call(this, i, this[i]) === false) {
                    return;
                }
            }
        }
    }
    
    ProductArray.iterateRemoved = function(fn) {
        for (var i = 0; i < this.length; i++) {
            if (this[i].removed) {
                if (fn.call(this, i, this[i]) === false) {
                    return;
                }
            }
        }
    }
    
    ProductArray.getSubLength = function(removed) {
        var cnt = 0;
        for (var i = 0; i < this.length; i++) {
            if (removed == this[i].removed) {
                ++cnt;
            }
        }
        return(cnt);
    }
    

    And, you would use them like this:

    ProductArray.iterateAdded(function(i, val) {
       // this is the array
       // i is the index we are iterating
       // val is the array element we are iterating this[i]
       // put code here
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays that I would like to compare and ultimately wind up
I have two arrays. I'd like to merge them into one, but remove the
I have two sets of arrays like this for example. $Arr1['uid'][]='user 1'; $Arr1['weight'][]=1; $Arr1['uid'][]='user
I have two arrays, each containing strings. The first array is a list of
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
Let's say I have two Arrays in Ruby, containing user IDs. These are the
I have two arrays tags and selected the tags array is static and depending
I'm having a little problem modifying an array in C. I have two arrays,
I have two arrays that need to be merged together and trying to figure
I have two arrays of data that I would like to display in an

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.