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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:36:32+00:00 2026-06-08T11:36:32+00:00

I am trying to remove elements from arrays while remembering their position and add

  • 0

I am trying to remove elements from arrays while remembering their position and add them back later. So far I have this piece of code:

var my_array = ['A', 'B', 'C', 'D', 'E'];
var removed_elements = [];

// Assuming letter provided exists in my_array
function remove_element(letter) {
    for (var index in my_array) {
        if (my_array[index] == letter) {
            break;
        }            
    }
    var removed_element = {
        index: index,
        letter: letter            
    }
    removed_elements.push(removed_element);
    my_array.splice(index,1);
}

// Assuming letter provided exists in removed_elements
function add_element(letter) {
    for (var index in removed_elements) {
        console.log('test');
        if (removed_elements[index].letter == letter) {
            console.log(removed_elements[index]);
            break;
        }            
    }
    my_array.splice(removed_elements[index].index,0,removed_elements[index].letter);
}

It works fine as long as I remove 1 element at a time and add it back before removing another one. However, when I start removing several elements consecutively, the index saved for removed elements (not first one, but subsequent ones) becomes relative to the state of my_array at the time of removal, and not absolute to my_array‘s initial state, which can cause problems.

For instance if you remove 'B' and 'D' and add 'D' and 'B', you end up with ['A', 'B', 'C', 'E', 'D'] instead of ['A', 'B', 'C', 'D', 'E'].

Here is a jsfiddle showing what the problem is

What modifications should I change for my_array to end up in its initial state no matter of many elements I remove or add and how?

I thought about storing information about which elements surround the removed element at the time of removal and use that as extra info when adding back but wondering if there is a better way.

  • 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-08T11:36:33+00:00Added an answer on June 8, 2026 at 11:36 am

    I wouldn’t actually remove them at all:

    var my_array = ['A', 'B', 'C', 'D', 'E'];
    var removed = [];
    
    function remove_element(letter) {
        var i = my_array.indexOf(letter);
    
        if(i > -1) {
            removed.push(i);
        }
    }
    
    function add_element(letter) {
        var i = my_array.indexOf(letter);
    
        if(i > -1) {
            removed.splice(removed.indexOf(i), 1);
        }
    }
    
    function get_array() {
        return my_array.filter(function(x, i) {
            return removed.indexOf(i) === -1;
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add and remove elements from a list mapped as .HasMany(),
I am trying to remove all HTML elements from a String. Unfortunately, I cannot
I'm trying to remove event listeners from elements after they've been clicked on and
I'm trying to remove a class attribute from an element and add the class
I'm trying to load page and then remove some elements in it. While doing
Let's say I am trying to remove elements from array a = [1,1,1,2,2,3] .
I am trying to remove all script elements from a HTML page. But for
I am trying to remove elements from an array if a value in that
I am trying to remove duplicate elements from an excel array. I imagine I'm
I am trying to remove elements from a Dictionary<string, List<string>> in C# when the

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.