如何存储对象数组? [英] How do I store an array of objects?
问题描述
我有一个对象图像
我可以执行 image.top
之类的操作,它会返回一个值,或者我可以 image.myPoints [0] .left
,它会返回一个值。我基本上有这个图像对象为我存储值。我希望能够在一个数组中放置多个 image
对象,这样我就可以这样做:
I have an object image
I can do things like image.top
and it will return a value, or I can do image.myPoints[0].left
and it will return a value. I basically have this image object that stores values for me. I want to be able to put multiple image
objects in an array so i could do something like this:
$("#toPinpoint").mapImage({
useAjax: false,
pinpoints: [ { "top": 50,
"left": 280,
"width": 200,
"height": 200},
{ "top": 0,
"left": 0,
"width": 300,
"height": 74 } ]
});
我使用此函数创建对象,将针点添加到对象上。当调用mapImage函数时,会发生这种情况:
I use this function to create the object, the pinpoints get added on to the object. When the mapImage function is called this is what happens:
$.fn.mapImage = function(options){
//Set user input options and defaults
var optionConfig = $.extend({}, $.fn.mapImage.defaults, options);
image=this;
this.image = this;
// Assign defaults
this.previewMode = optionConfig.previewMode;
this.pinpoints = optionConfig.pinpoints;
image.pinpoints = optionConfig.pinpoints;
image.pinpointCount = 0;
image.selected = 0;
...}
这设置图像属性,现在我想修改特性使用我的应用程序然后将这些图像
对象保存到数组中。
This sets the image properties and now I want to modify the properities with my application then SAVE these image
objects into an array.
我的问题是数组正在加载使用图像对象,但它似乎用我刚刚推入的对象填充整个数组,因此它不会保存我的旧图像对象。例如,如果我执行 myArray [0] .myPoints [0] .left
,让我们说它重新打造30,然后我推送另一个具有myPoints [0]的图像对象.left等于20,我在阵列中的第一个图像对象变成20而不是保存为30.如果这是一个解决这个问题的好方法,我将不胜感激。谢谢!
My problem with this is that the array is loading up with image objects, but it seems to fill the whole array with the object i just pushed in, so it doesnt save my old image objects. For example, if i do myArray[0].myPoints[0].left
, lets say it retruns 30, and then i push another image object that has myPoints[0].left equal to 20, the first image object I have in the array turns into 20 instead of saving it as 30. If theres a good way of solving this issue it would be greatly appreciated. Thank you!
推荐答案
使用 array.push(x)
应该添加 x
到数组
的末尾。但是,它不会创建 x
的副本,因此如果您更改 x
和<$ c的属性$ c> push()它再一次,你的数组将包含两个带有更新属性的 x
实例 - 我怀疑这就是你所看到的。 (也就是说,您可能正在更改图像的
和顶部
属性 push()
再次使用它,而不是创建一个全新的图像
到 push()
)。
Using array.push(x)
should add x
to the end of array
. However, it will not create a copy of x
, so if you then change an attribute of x
and push()
it again, your array will contain two instances of x
with the updated attribute - I suspect this is what you are seeing. (That is, you may be changing the top
property of your image
and push()
ing it again, instead of creating a whole new image
to push()
).
这篇关于如何存储对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!