0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

aha, thanks for point me out that line, i am searching for almost half a day but cant find that. thanks mate.
@JosephGoh glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.