Overwrite array in JavaScript
Asked Answered
M

6

5

How do I overwrite (or unset and then set) an array? Seems like "array = new_array" doesn't work.

Mouser answered 12/10, 2009 at 16:47 Comment(1)
Can you please provide a code sample for us to see why array = new_array code is not working?Deadline
M
-2

Hm, it seems like the problem wasn't what I thought; my mistake was the following rows, which after all havn't got anything to do with arrays at all:

sms.original = eval('(' + data + ')');
sms.messages = sms.original;

sms.original becomes an object, and then sms.messages becomes sms.original (I just wanted them to have the same value). The objects contain an array named items which was ment to remain static in the sms.original object, but when I changed sms.messages the original object changed as well. The solution was simple:

sms.original = eval('(' + data + ')');
sms.messages = eval('(' + data + ')');

Sorry for bothering you, I should have elaborated but the code is splited in multiple files and functions. Thank you guys anyway, now does Guffa's splice technique work for me.

Mouser answered 12/10, 2009 at 19:21 Comment(0)
C
14

To create an empty array to assign to the variable, you can use the Array constructor:

array = new Array();

Or you can use an empty array literal:

array = [];

If you have several references to one array so that you have to empty the actual array object rather than replacing the reference to it, you can do like this:

array.splice(0, array.length);
Currish answered 12/10, 2009 at 16:51 Comment(0)
C
4

Clearing an array

http://2ality.com/2012/12/clear-array.html

let myArray = [ 1, 2, 3, 4];

myArray = [];
myArray.length = 0;
Croupier answered 9/8, 2017 at 6:47 Comment(1)
If you need to clear out an array without breaking the reference, myArray.length = 0 is what you're looking for!Deliquescence
P
3

This should work.

array1 = array2;

If not, please provide more details.

Parturifacient answered 12/10, 2009 at 16:52 Comment(2)
I don't think you need to explicitly state array1 = null. If you just update the reference, the garbage collector should recognize an instance that has no references pointing to it.Anticathode
No need for two assignments; the second one is enough.Krueger
A
0

I'm not exactly sure what you're trying to do, but there are a couple of ways to go about resetting an array.

You could just iterate through the existing array and set each index equal to null (or an empty string or 0 or whatever value you consider to be a reset):

for(var i = 0; i < arr.length; i++) {
   arr[i] = null;
}

You could also just update the existing reference to a new instance of an object:

arr = [];
Anticathode answered 12/10, 2009 at 16:52 Comment(0)
P
0

Using Slice()

like this -> array = new_array.slice(0);

Properly answered 12/11, 2019 at 10:12 Comment(0)
M
-2

Hm, it seems like the problem wasn't what I thought; my mistake was the following rows, which after all havn't got anything to do with arrays at all:

sms.original = eval('(' + data + ')');
sms.messages = sms.original;

sms.original becomes an object, and then sms.messages becomes sms.original (I just wanted them to have the same value). The objects contain an array named items which was ment to remain static in the sms.original object, but when I changed sms.messages the original object changed as well. The solution was simple:

sms.original = eval('(' + data + ')');
sms.messages = eval('(' + data + ')');

Sorry for bothering you, I should have elaborated but the code is splited in multiple files and functions. Thank you guys anyway, now does Guffa's splice technique work for me.

Mouser answered 12/10, 2009 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.