Reputation: 733
I am creating an apps that will loop all necessary field and store as object and pass it to backend via http.
I am using array push to add it to the object while looping.
Here is the code in JS.
JS
var form_data_body = [];
for (var k = 0; k < $scope.Tablelist.length; k++) {
if ($scope.Tablelist[k].selected == true) {
if ($scope.Tablelist[k].approve == "Y") {
var Suppno = $scope.Tablelist[k].supp_no;
var Price = $scope.Tablelist[k].unit_price;
if (Suppno != "") {
if (suppliersebelum == "") {
suppliersebelum = Suppno;
} else {
if (suppliersebelum != Suppno) {
continue;
}
}
if (Price > 0) {
var Matcode = $scope.Tablelist[k].matcode;
var Poqty = $scope.Tablelist[k].pr_qty;
var Prprice = $scope.Tablelist[k].unit_price;
var Priceid = $scope.Tablelist[k].price_id;
var Dept = $scope.Tablelist[k].req_dept;
var Refno = $scope.Tablelist[k].reff;
var ReqDate = $filter('date')(new Date($scope.Tablelist[k].date_req), 'yyyy-MM-dd');
var Tanggal = $filter('date')(new Date(), 'yyyy-MM-dd');
console.log(k);
form_data_body.push = {
matcode: Matcode, po_qty: Poqty, unit_price: Prprice, etd_date: ReqDate, dept_no: Dept,
priceid: Priceid, ref_no: Refno
};
}
} else {
console.log("failed");
}
} else {
console.log("failed");
}
}
}
console.log(Object.keys(form_data_body).length);
console.log(JSON.stringify(form_data_body));
Those code above will be fired when i click on button and will be through a tablelist, however no matter how much it meet the conditions, the onsole.log(Object.keys(form_data_body).length);
will show 1
and console.log(JSON.stringify(form_data_body));
will show []
and console.log(k);
will show number of looping.
is there something wrong with the code that caused it cannot push to the form_data_body
object?
Upvotes: 0
Views: 847
Reputation: 68393
Replace this line
form_data_body.push = {
matcode: Matcode, po_qty: Poqty, unit_price: Prprice, etd_date: ReqDate, dept_no: Dept,
priceid: Priceid, ref_no: Refno
};
with
form_data_body.push ( {
matcode: Matcode, po_qty: Poqty, unit_price: Prprice, etd_date: ReqDate, dept_no: Dept,
priceid: Priceid, ref_no: Refno
});
Push is a method which takes item as parameter rather than assignment.
Upvotes: 1