[英]Push values to array in jquery
I have List of items data and empty array quotations
: 我有项目列表data and empty array quotations
:
var data = {};
var quotations = [];
I want to fill quotations
with data values ,Every time i add new data it added successfully but all data values get last value . 我想用数据值填充quotations
,每次添加新数据时,它都会成功添加,但所有数据值都会获得最后一个值。
for example : 例如 :
$("#addquotation").click(function () {
debugger;
var itemname = $("#itemname").val();
var cost =parseFloat( $("#cost").val());
var notes = $("#notes").val();
var date = $("#date").val();
data.Item = itemname;
data.Cost = cost;
data.Notes = notes;
data.Date = date;
quotations.push(data);
)};
for first time i add 我第一次添加
"test,45,testnotes,2016-02-03" Second time i 've added "test2,45.2,testnotes2,2016-02-05" “test,45,testnotes,2016-02-03”第二次我添加了“test2,45.2,testnotes2,2016-02-05”
when i debug i get data as : 当我调试我获取数据为:
obj(0): "test2,45.2,testnotes2,2016-02-05" obj(1):"test2,45.2,testnotes2,2016-02-05" obj(0):“test2,45.2,testnotes2,2016-02-05”obj(1):“test2,45.2,testnotes2,2016-02-05”
it seems it append last version to all data 它似乎将最后一个版本附加到所有数据
Please Advice . 请指教 。 Thanks 谢谢
You need to declare data inside the click handler, if it's declared as a global variable you are basically always modifying and adding the same data object to the array: 您需要在click处理程序中声明数据,如果它被声明为全局变量,您基本上总是在修改并向数组添加相同的数据对象:
var quotations = [];
$("#addquotation").click(function () {
debugger;
var data = {};
var itemname = $("#itemname").val();
var cost =parseFloat( $("#cost").val());
var notes = $("#notes").val();
var date = $("#date").val();
data.Item = itemname;
data.Cost = cost;
data.Notes = notes;
data.Date = date;
quotations.push(data);
)};
You are pushing the same object reference each time since you declared data
outside of the click handler. 由于您在单击处理程序之外声明data
,因此每次都推送相同的对象引用。
Change from : 改变自:
var data={};
$("#addquotation").click(function () {
To 至
$("#addquotation").click(function () {
var data={};// declare local variable
The problem is that data
is a global variable and you add a reference to data
to quotations
. 问题是data
是一个全局变量,您可以将data
引用添加到quotations
。
When the first value is pushed to quotations
, data
and quotations[0]
refer to the same object. 当第一个值被推送到quotations
, data
和quotations[0]
引用同一个对象。 Here is an example of what is happening: 这是一个正在发生的事情的例子:
var a = {num: 1};
var b = a;
b.num = 2;
console.log(a.num); // prints 2
The same thing happens when an object is pushed to an array. 将对象推送到数组时会发生同样的事情。 quotations
does not contain a copy of data
, it contains a reference to data
so that modifying data
also modifies quotations
. quotations
不包含data
副本,它包含对data
的引用,以便修改data
也可以修改quotations
。 To fix this, each element of quotations
must refer to a different data object. 要解决此问题, quotations
每个元素都必须引用不同的数据对象。 This can be accomplished by defining data
inside of the function instead of outside. 这可以通过在函数内部而不是在外部定义data
来实现。
Replace 更换
var data = {};
$("#addquotation").click(function() {
// populate data, push to quotations
});
with 同
$("#addquotation").click(function() {
var data = {};
// populate data, push to quotations
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.