Javascript 将值推送到 jquery 中的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36119159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push values to array in jquery
提问by prime
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
请指教 。谢谢
回答by HaukurHaf
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:
您需要在点击处理程序中声明数据,如果它被声明为全局变量,您基本上总是在修改相同的数据对象并将其添加到数组中:
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);
)};
回答by charlietfl
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
回答by afuous
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. Here is an example of what is happening:
当第一个值被推送到quotations
,data
并quotations[0]
引用同一个对象时。这是正在发生的事情的一个例子:
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
. To fix this, each element of quotations
must refer to a different data object. This can be accomplished by defining data
inside of the function instead of outside.
当一个对象被推送到一个数组时,也会发生同样的事情。quotations
不包含 的副本data
,它包含对的引用,data
以便修改data
也会修改quotations
。要解决此问题, 的每个元素都quotations
必须引用不同的数据对象。这可以通过data
在函数内部而不是外部定义来实现。
Replace
代替
var data = {};
$("#addquotation").click(function() {
// populate data, push to quotations
});
with
和
$("#addquotation").click(function() {
var data = {};
// populate data, push to quotations
});