javascript数组推空键和值

时间:2016-05-20 06:15:52

标签: javascript arrays

我正在创建一个应用程序,它将循环所有必需的字段并存储为对象,并通过http传递给后端。

我正在使用数组推送在循环时将其添加到对象。

以下是JS中的代码。

JS

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Log In")));
        WebElement logIN = driver.findElement(By.linkText("Log In"));
        logIN.click();

当我点击按钮并将通过表格列表时,上述代码将被触发,但无论条件多少,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)); 都会显示onsole.log(Object.keys(form_data_body).length);1将显示console.log(JSON.stringify(form_data_body));[]将显示循环次数。

代码是否有问题导致它无法推送到console.log(k);对象?

1 个答案:

答案 0 :(得分:1)

替换此行

  form_data_body.push = {
        matcode: Matcode, po_qty: Poqty, unit_price: Prprice, etd_date: ReqDate, dept_no: Dept,
        priceid: Priceid, ref_no: Refno
      };

    form_data_body.push ( {
        matcode: Matcode, po_qty: Poqty, unit_price: Prprice, etd_date: ReqDate, dept_no: Dept,
        priceid: Priceid, ref_no: Refno
      });

Push是一种将item作为参数而不是赋值的方法。