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.

5 Answers5

0

To unset an object property you can use the "delete" operator. For example:

var f ={
    test1: "fire",
    test2: "water"
}
console.log(f.test2)
//Output water
delete f.test2;
console.log(f.test2)
//Output undefined

To resolve your overall problem you could use something similar to.

this.cache['A'] = new Orange(1);
this.cache['B'] = new Orange(2);
this.cache['C'] = new Orange(3);
this.cache['D'] = new Orange(4);
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
CBusBus
  • 2,321
  • 1
  • 18
  • 26
  • Your "resolved" code is what is stated in the question itself. Not able to understand the difference! – rsmoorthy Jan 27 '12 at 18:07
  • Having read over it I can say there is no real difference; it just so happens that the op posted answer to his own question. – CBusBus Jan 28 '12 at 17:28
0

Are you sure that the object reference is no more with E?

This code:

var cache = {};
cache['B'] = new Object( {"hello": "world"} );
cache['E'] = cache['B'];
delete cache['B'];

Now cache['E'] still contains the object, that was assigned to cache['B'].

> cache['E'].hello
"world"
>
rsmoorthy
  • 2,284
  • 1
  • 24
  • 27
0

Is the following sufficient?

var myArray= ['a','b','c','d'];
myArray[4] = myArray[1];
myArray.splice(1,1)
var stuff = myArray.join("");
document.write(stuff)

Result: acdb

Take a look at this post

Community
  • 1
  • 1
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
0

I hope you are using object not array. There is no property length for object in Javascript.

As array. Here even if it is declared as array, values are not added to the array ( like in php etc. ) . Instead added as properties. So length will be always zero.

this.cache = [];
this.cache['A'] = 1;
this.cache['B'] = 2;
this.cache['C'] = 3;
this.cache['D'] = 4;
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
console.log(this.cache['B'], this.cache.length, this.cache.hasOwnProperty('B'));

As you can see in the above example this.cache is not used as an array. So you can create and object.

this.cache = {};
this.cache['A'] = 1;
this.cache['B'] = 2;
this.cache['C'] = 3;
this.cache['D'] = 4;
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
console.log(this.cache['B'], this.cache.length, this.cache.hasOwnProperty('B'));

In both cases hasOwnProperty('B') will return false. If the object has the property B, hasOwnProperty will return true. Your first method should work.

http://jsfiddle.net/diode/hfLJz/1/

.

Diode
  • 24,570
  • 8
  • 40
  • 51
0

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-
Olegas
  • 10,349
  • 8
  • 51
  • 72
  • I was trying to access the newly created element by the wrong index (a[3] in your case). After an array element has been sliced, all the indices are shifted, so delete index "1" will shit indices "2" to "1" and "3" to "2". I was trying to work with an array like an associative array which was why it had problems. –  Feb 05 '12 at 12:33