[英]Pushing objects to an array only returns last object pushed
I know this question asked before many time and I have researched a lot but I couldn't find the solution. 我知道这个问题已经问了很多次了,我进行了很多研究,但是找不到解决方案。 So, I have an form and I am getting the form values to data array with
form.serializeArray();
因此,我有一个表单,并且正在使用
form.serializeArray();
将表单值存储到数据数组中form.serializeArray();
And I create an object obj
with this array. 我用这个数组创建一个对象
obj
。 I need to push every object to new array personArray
. 我需要将每个对象推送到新的数组
personArray
。 But pushing objects in this array only returns last object pushed. 但是在此数组中推送对象只会返回最后推送的对象。 How can I solve this problem.
我怎么解决这个问题。 I need to store every new object which comes from form in that
personArray
我需要将来自表单的每个新对象存储在该
personArray
var obj = {}; var data, i; var personArray = []; var pageArray = []; $("#submitButton").click(function() { addButton(); // addTable(); // pagination(); // resetForm(''); }); /*Formdaki verileri objeye aktarır, objeyi array'a aktarır*/ function addButton() { data = $('#personForm').serializeArray(); for (i = 0; i < data.length; i++) { obj[data[i].name] = data[i].value; } personArray.push(obj); console.log(personArray); /*Datayı array'e aktarır*/ }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <form id="personForm"> <div> <label for="firstname">İsim:</label> <input type="text" id="firstname" name="firstname" value="" placeholder="İsim" /> </div> <div> <label for="lastname">Soyisim:</label> <input type="text" id="lastname" name="lastname" value="" placeholder="Soyisim" /> </div> <div> <label for="tc">TC:</label> <input type="tel" id="tc" name="tc" value="" placeholder="TC Kimlik No" /> </div> <div> <label for="birthday">Doğum T:</label> <input type="date" id="birthday" name="birthday" /> </div> <div> <input type="button" value="Kaydet" id="submitButton" /> </div> </form>
You are using the same object, so every time you are overiding the already pushed object and re-push it again. 您使用的是同一对象,因此,每次覆盖已推送的对象并再次将其重新推送时,都将使用它。 Since object are just a reference to a space in memory, you have to create a new object each time or you'll only end up with the same object pushed many times.
由于对象只是对内存中空间的引用,因此您每次都必须创建一个新对象,否则最终只会多次压入同一对象。 Here is an explanation of what is happening:
这是正在发生的情况的解释:
var arr = []; var o = {}; o["data"] = "hello"; arr.push(o); o["other"] = "world"; arr.push(o); console.log(arr);
Here is what you should do: 这是您应该做的:
var hash = {}; // the hash object
var data, i;
var personArray = [];
var pageArray = [];
$("#submitButton").click(function() {
addButton();
// addTable();
// pagination();
// resetForm('');
});
function addButton() {
var obj = {}; // create a brand new object every time
data = $('#personForm').serializeArray();
for (i = 0; i < data.length; i++) {
obj[data[i].name] = data[i].value;
hash[data[i].name] = data[i].value; // assign to the hash object
}
personArray.push(obj);
console.log(personArray);
}
try This ! 尝试这个 !
var data, i;
var personArray = [];
var pageArray = [];
$("#submitButton").click(function() {
addButton();
// addTable();
// pagination();
// resetForm('');
});
/*Formdaki verileri objeye aktarır, objeyi array'a aktarır*/
function addButton() {
var obj = {};
data = $('#personForm').serializeArray();
for (i = 0; i < data.length; i++) {
obj[data[i].name] = data[i].value;
}
personArray.push(obj);
console.log(personArray);
/*Datayı array'e aktarır*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.