检索推送删除更新JSON数组内的嵌套数据

时间:2017-05-12 10:10:26

标签: arrays json angular

我有以下数据json:

"user" : {
   "id": 456,
   "nickname": "xxx",
   "pf": {
        "id": 123,
        "account": [
            {
                "accountid": 1494235749,
                "status": "New",
                "accountnbr": 12345,
                "cyc": {
                    "cycid": 1494404053,
                    "active": true,
                    "status": "New",
                    "name": "QST192017",
                },

            },
            {
                "accountid": 1494403399,
                "status": "New",
                "accountnbr": 915177,
                "cyc": {
                    "cycleid": 1494406299,
                    "active": true,
                    "status": "Closed",
                    "name": "QSL342014"
                }
            }
        ]
    },
 }

我可以使用以下行检索angular2中的json数据:

this.user = this.userService.user;

也适用于user.pf和user.pf.account(所有帐户)

以下是我的问题:

  1. 如何获得所有的循环?
  2. 如何添加新帐户/ cyc?
  3. 如何删除帐户/ cyc?
  4. 请注意,每个帐户只有一个cyc。 我使用angular2。

    谢谢。

    / K

1 个答案:

答案 0 :(得分:0)

  

如何获得所有的循环?

import UIKit import AWSDynamoDB class Item: AWSDynamoDBObjectModel, AWSDynamoDBModeling { var id: String? var category: String? var name: String? class func dynamoDBTableName() -> String { return "Items" } class func hashKeyAttribute() -> String { return "id" } } 保留所有帐户,并且cyc也属于它。你可以循环浏览this.userService.user.pf.account

this.userService.user.pf.account
  

如何添加新帐户/ cyc?

// keep cyc in new array
var cycs = [];
this.userService.user.pf.account.forEach(function(account) {
  // each cyc of account can be got here
  console.log(account.cyc);
  cycs.push(account.cyc);
});
  

如何删除帐户/ cyc?

this.userService.user.pf.account.push(newAccount);   // add new account
this.userService.user.pf.account.cyc = newCyc;       // suppose the current account don't have one cyc, if it has one then will be overwritten