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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:29:41+00:00 2026-05-28T15:29:41+00:00

Is it possible to swap a reference of an object that an array element

  • 0

Is it possible to swap a reference of an object that an array element contains to another index and remove/delete the old index.

Example:
I have an array of OBJECTS with indexes A, B, C, D.

Now I want to create a new array element with index F and assign it the reference of an object that index B contains. After that, I want to remove B from the array so that only A, C, D, E remains.

What I basically want to do is copy the reference of an object that index B contains and copy it to E.

I have tried the code below but it doesn’t work:

this.cache['A'] = new Orange(1);
this.cache['B'] = new Orange(2);
this.cache['C'] = new Orange(3);
this.cache['D'] = new Orange(4);

// I want to pass the reference B contains
// and assign it to F (like something you can do in C++)
this.cache['E'] = this.cache['B'];  

// First attempt
// length of array is 5 (cache['B'] just has value of undefined)
delete this.cache['B'];

// Second attempt
// the reference to object Orange was deleted in both B and E
this.cache.splice('B', 1);

I don’t want to create a new object and reassign the values, because there are many references and bindings to the objects, so doing a deep copy would be meaningless.

  • 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-28T15:29:43+00:00Added an answer on May 28, 2026 at 3:29 pm

    I’m wondering why the “splice” method is not the solution.

    Look at my sample code. I’m using splice there and no any references is lost…

    function Oo(s) {
        this._s = s;
    }
    
    Oo.prototype.toString = function() {
        return "-" + this._s+"-";
    }
    
    var a = [];
    a.push(new Oo('x'));
    a.push(new Oo('y'));
    a.push(new Oo('z'));
    
    a[3] = a[1];
    alert(a);  // -x-,-y-,-z-,-y-
    
    a[3]._s = 'XX'; // let's change the newly added item
    alert(a); // -x-,-XX-,-z-,-XX- <- 3 and 1 points to the same object!
    
    a.splice(1, 1); // remove one element, starting from index = 1
    alert(a); // -x-,-z-,-XX-
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that contains, as part of its data, a linked list.
Possible Duplicate: Javascript swap array elements I have a array like this: this.myArray =
I want to swap to tables in the best possible manner. I have an
Is it possible tell the JVM to hint the OS that a certain object
I am working on a PHP application that finds all possible ways to swap
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Since we have rvalue references in C++0x it seems to me that it should
is it possible to swap primary key values between two datasets? If so, how
Is it possible to alter css class with jQuery? For instance if i have
Like in the title I want to swap the field that is rendered on

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.