将HTML表映射到JSON数组

时间:2018-07-17 13:39:24

标签: javascript html json

我是前端的新手,使用JavaScript可以完成非常简单的任务。长话短说,我有一张桌子:

<table class="table table-striped table-bordered table-hover">
  <tr>
    <th style="width:80%; text-align: center;">Account number</th>
    <th></th>
  </tr>
  <tr ng-repeat="account in model.accounts track by $index">
    <td>{{ account}}</td>
    <td style="text-align: center;"><input type="button" value="Remove" ng-click="removeAccount($index)" class="btn" /></td>
  </tr>
</table>

我需要将这些值映射到如下所示的JSON中:

"Accounts": {
  "number": "model.accounts",
  "otherinfo": "default_value"
}

这时我只有数字,otherinfo稍后会添加。

我尝试过这样的事情:

this.$scope.model= {
  accounts: []
}

我已经检查过了,它保存了我已放入表格中的所有帐号。

现在,我需要从中制作一些帐户对象的数组,稍后将在JSON.stringify()内部使用,我尝试过此操作:

var accounts = [{
  number: "",
  otherinfo: "",
}]

var x;
for (x = 0; x < $scope.model.accounts.length; x++) {
  accounts[x].number = $scope.model.accounts.nrbList[x];
}

例如,如果我向表中添加2个元素,则我希望我的数组包含2个对象,都具有表中的值和otherinfo的默认值,该值是一个空字符串。

不幸的是,我得到了:

TypeError: Cannot set property 'number' of undefined

0 个答案:

没有答案