Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by jswart

What is the deal here?

array1 = [ 1, 2, 3];

array2 = array1.splice();

array3 = array2.splice();

console.log(array3);       // displays: []       ie: an empty array

What is going on here?

1 decade ago by alexandre

Incorrect usage, I think. See the splice docs. An index is expected, with other arguments optional.

(untested)
array1 = [1,2,3];
array2 = array1.splice(0,1);   // array2: [2,3]
array3 = array2.splice(0,1);   // array3: [3]

1 decade ago by jswart

The answer ended up being:
arrayN.splice(0);

But that still works by reference. I created this function:

function copyArray(anArray) 
{
	var aReturn = [];
	for (var i = 0; i < anArray.length; i++) {
		aReturn.push(anArray[i]);
	};
	return aReturn;
}

Does this exist anywhere in javascript natively? I have to say, arrays in JavaScript are horrible to me. PHP may be a lot of things, but it does arrays well.

1 decade ago by Philip

If your array only contains types like Number, Bool or String the splice(0) should work.

But if there are arrays or objects in the array, they get passed by as a reference. If you need a deep copy, use the ig.copy function provided by impact.

There is no nativ way to do this.

1 decade ago by StuartTresadern

array.splice() returns a new array of the REMOVED elements.


So array2 will be = 1 and array1 will be 2,3 after the splice

1 decade ago by jswart

Quote from StuartTresadern
array.splice() returns a new array of the REMOVED elements.


So array2 will be = 1 and array1 will be 2,3 after the splice


Sort of true. Using:

array1 = [1, 2, 3];

array2 = array1.slice(0);

console.log(array1, array2) // displays: [1, 2, 3] [1, 2, 3]

Will copy the array. The issue I was having is that I left out the 0. I confused usage of slice() with concat().

Either way objects in array elements are still 'copied' by reference as someone pointed out, and I need an actual copy.

But if you are working with ints, floats, strings you are okay.

1 decade ago by KirbySaysHi

The best way to copy an array is with .slice(), which implicitly returns a new array that can be mutated.

Since JS works by reference on any non-primitive value, if you want to do what's called a deep copy, then things quickly, quickly get complicated. I suggest just loading jquery, and use the second form of jQuery.extend (http://api.jquery.com/jquery.extend):

var orig = [ [ 0, 1, 2 ], 'yes' ], other = [];
jQuery.extend(true, other, orig); // true signifies deep
other[0].pop();
console.log(other[0].length) // should be 2
console.log(orig[0].length) // should still be 3

But I have to ask, is there a reason you need to actually copy the array?

1 decade ago by Philip

You don't need jQuery, impact has the same functionality

ig.copy( [] )

1 decade ago by jswart

Quote from KirbySaysHi

But I have to ask, is there a reason you need to actually copy the array?


Yes. I need to know the values of an array of objects, before and after modification to test if anything has changed.

1 decade ago by KirbySaysHi

You might benefit from a small experiment of mine, called StackMemory. It lets you watch any objects for change, and also save and restore the values, in the event that you want to modify temporarily.

https://gist.github.com/3123691

I believe it will do arrays just fine, if you set the array paths without using []:

this.pm = new StackMemory(['pos.x', 'pos.y', 'elements.0'], this);

// later
this.pm.diff() // returns what's changed

// and then when you want to set the current changes as the "original"
this.pm.reread();

If you don't use it, maybe it at least could give you some ideas.

1 decade ago by jswart

Quote from KirbySaysHi
You might benefit from a small experiment of mine, called StackMemory. It lets you watch any objects for change, and also save and restore the values, in the event that you want to modify temporarily.

https://gist.github.com/3123691

I believe it will do arrays just fine, if you set the array paths without using []:

this.pm = new StackMemory(['pos.x', 'pos.y', 'elements.0'], this);

// later
this.pm.diff() // returns what's changed

// and then when you want to set the current changes as the "original"
this.pm.reread();

If you don't use it, maybe it at least could give you some ideas.


Thanks, I'll give it a look!

1 decade ago by alexandre

@KirbySaysHi
Useful tool. Thanks for this. Will come in handy at some point.
Page 1 of 1
« first « previous next › last »