开发者

How do I store different states of an object in an array?

I have an object called apples and if i do something like apples[0].weight it returns a value. Now I want to be able to store an array of apples. Lets call the array myApples. How do I add multiple instances of apples to the myApples array without the values changing to the most updated one. For example:

apples[0].weight = 10;
apples[1].weight = 20;
myApples.push(apples);
apples[0].weight = 15;
apples[1].weight = 16;
myApples.push(apples);

Now if I were to print the value myApples[0].apples[0].weight i get 15 instead of getting 10. I think its because the reference is the same array, so it gets updated every time I change it. How would I go about storing these apple "states" so I can keep a history of changes made to it. Keep in mind I want to make this arbitrary, so I wouldnt have to make something like apples1, apples2, apples3, because the changes on this could be unlimited.(In my actual project I am storing pinpoints on a canvas, so i can basically do an "undo" and "redo" feature and want to store the values of these pinpoints every time something changes on the canvas.)

EDIT- This is how I make apples or pinpoints:

var options = {pinpoints: [ { "top": 50,
                           "left": 280,
                           "width": 20开发者_运维技巧0,
                           "height": 200},
                         { "top": 0,
                           "left": 0,
                           "width": 300,
                           "height": 74 } ]}    
var optionConfig = $.extend({}, $.fn.mapImage.defaults, options);
var myPinpoints = optionConfig.pinpoints;

So if i do myPinpoints[0].left it gives me 280, I want to be able to push myPinpoints into an array then change myPinpoints[0].left to something else, and then push it on to the array again and save BOTH values


You can use jQuery.clone kind of API to clone the Object and move it to your Stack or Queue. This way you can ensure object states are maintained separately.


Well what is it that you need "apples" for anyway?

myApples.push([
  { weight: 10 },
  { weight: 20 }
]);
myApples.push([
  { weight: 15 },
  { weight: 16 }
]);

Now if there's more to an apple than just "weight", maybe you need an apple-maker function:

function makeApple(weight, variety, color) {
  return {
    weight: weight,
    variety: variety || "Braeburn",
    color: color || "red"
  };
}

Then you can do this:

myApples.push([
  makeApple(10, "Fuji"),
  makeApple(20, "Granny Smith", "green")
]);

or whatever.

edit — as Felix King points out, the nature of "apples" isn't super clear. In any case, whatever "apples" is, personally I would lean towards making a more function-oriented setup to create the objects, or arrays, or arrays of objects, or whatever they are.


I think that the object apples is reference based. So even though you push them onto your array, when you change the value to 15, you are still changing the first apples object. Try using the keyword 'new'.

var apples1 = new int[99];
var apples2 = new int[99];

apples1[0].weight = 10;
apples1[1].weight = 20;
myApples.push(apples1);
apples2[0].weight = 15;
apples2[1].weight = 16;
myApples.push(apples2);


Anthony,

Why not use 2-dimensional arrays instead?. e.g.

var apples = new Array(100);
apples[0]  = new Array(2);
apples[0][0] = {weight: 0};
apples[0][1] = {weight: 1};
apples[1]  = new Array(2);
apples[1][0] = {weight: 10};
apples[1][1] = {weight: 11};

Regards Neil


Now that I understand a little more here is how I would do it. First define a coordinates object that contains x,y inputs. Next setup a method to stack these pinpoints / coordinates, I am assuming you are working on an x,y grid. I am loosely writing this code and not testing so take it with a grain of salt.

    // declare an array in global scope
var CoordinateCollection = new Array();

// create an object model
function Coordinate(x,y) { 
    this.x = x;
    this.y = y;
}

// this function will push a new coordinate onto the array
function AddCoordinate(x,y) { 
    CoordinateCollection.push(new Coordinate(x,y));
}

// after calling AddCoordinate you could now
// say something like
// alert(CoordinateCollection[0].x);
// if you call it again you could say
// alert(CoordinateCollection[1].x);
// and it would have a different value if the
// two original values were different.

EDIT...

You could just omit the call to AddCoordinate and use the line inside of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜