Results 1 to 4 of 4
  1. tucker87's Avatar
    Posts
    16 Posts
    Global Posts
    17 Global Posts
       #1  
    Searched the forums and couldn't find anything relevant. I am having an issue with Splicing an array.


    Here is a test program I wrote to make sure it wasn't interference from the app I was working on:

    Code:
    enyo.kind({
    name: "mainView",
    kind: enyo.VFlexBox,
    components: [
    		{kind: "PageHeader", content: "value", onclick: "nukeDB"},
    	],
    	create: function(launchParams) {
    		this.inherited(arguments);
    		this.run();
    	},
    	run: function() {
    		this.list = [{name: "test"}, {name: "123"}];
    		this.log(this.list);
    		this.listCopy = this.list;
    		//this.listCopy.splice(0, 1);
    		this.log(this.list);
    		this.log(this.listCopy);
    	}
    });
    As you can you see I log the list, create another variable (copy) then splice the copy, then log both.

    With the splice commented out, as seen here, the output is:

    Code:
    mainView.run():  [Object, Object]
    mainView.run():  [Object, Object]
    mainView.run():  [Object, Object]
    As you can see the same data is repeated three time, all of which have the data intact. However if I uncomment the splice then I get this:

    Code:
    mainView.run():  [Object]
    mainView.run():  [{"name":"123"}]
    mainView.run():  [{"name":"123"}]
    At all three points, even before the splice!, both list and listCopy have been spliced. I lose data.

    How can I make a copy of an array and keep the original from being spliced?
  2. tucker87's Avatar
    Posts
    16 Posts
    Global Posts
    17 Global Posts
       #2  
    Never liked splice anyway. Made this as a workaround:

    Code:
    removeElements: function(array, index) {
    		tempArray = [];
    		counter = 0;
    
    		for(i = 0; i < array.length; i++)
    		{
    			if(i != index)
    			{
    				tempArray[counter] = array[i];
    				counter++;
    			}
    		}
    		return tempArray;
    	},
  3. #3  
    listCopy is not a copy but a reference to the same object.
    If you change listCopy the original gets changed as well.
  4. #4  
    What somline said. Also, splice(0,1) could just be replaced with shift(). If you want to create a copy of an array, then you can use Prototype's Object.clone() or make your own. Basically, you just have to copy each array item over to the new array. Be aware though, if your array item is an object(or array) you'll have to do the same with it. For example.
    Code:
    anArray = [1, [2,3,4,5], 6];
    anotherArray = anArray;
    anotherArray[1] = 3;
    //anArray now equals [1,3,6];
    Code:
    anArray = [1,[2,3,4,5],6];
    anotherArray = [];
    for(var x = 0; x < anArray.length; x++){
      anotherArray[x] = anArray[x];
    }
    anotherArray[0] = 'ONE';
    //anArray now equals [1,[2,3,4,5],6]
    //anotherArray now equals ['ONE',[2,3,4,5],6]
    
    anotherArray[1] = 'TWO';
    //anArray now equals [1,'TWO',6]
    //anotherArray now equals ['ONE,'TWO',6]
    danNielson.com - Slalom - twitter - facebook
    A posted thanks dies with the thread. Clicking the button lasts until the admins get bored with this site and throw it in the recycling bin.

Posting Permissions