ManuKaracho
ManuKaracho

Reputation: 1218

Pushing Values from JSON Object into Array

I have the following object in my AngularJS Controller:

  {"team":"1","name":"abc","age":"20"},
  {"team":"1","name2":"def","age2":"21"},
  {"team":"2","name":"ghi","age":"22"},
  {"team":"2","name2":"jkl","age2":"23"},

I need to group the items into one array object, by the team key.

  {
   "team": [
     {"name1":"abc","age1":"20", "name2":"def", "age2":"21"},
     {"name1":"ghi","age1":"22", "name2":"jkl", "age2":"23"}
   ]
  }

So I can later use the information like $scope.data.team[1].name2

EDIT: One Team always consists of 4 players by the way. How would I do this?

Upvotes: 0

Views: 146

Answers (1)

Raphael Müller
Raphael Müller

Reputation: 2200

edit: working plunkr for your needs: http://plnkr.co/edit/zxoOYV?p=preview

you should rearrange your structure. i.e. you could go for something like this:

{"team": [
    {"players": [
        {"name" : "abc", "age": 20},
        {"name" : "def", "age": 34},
    ]},
    {"players": [
        {"name" : "abc", "age": 20},
        {"name" : "def", "age": 34},
    ]}
]}

if you use this structure in your controller:

$scope.team = {...}

and use it in your html like:

<div ng-controller="TeamController">
    <div ng-repeat="players in team">
       <div ng-repeat="player in players">
          <div>Name: {{player.name}}</div>
          <div>Name: {{player.age}}</div>
       </div>
    </div>
</div>

so, for your example, i got the angular-schema-form working.

with the above structure the schema looks like this:

[
  {
    "type": "help",
    "helpvalue": "<h4>Tabbed Array Example</h4><p>Tab arrays can have tabs to the left, top or right.</p>"
  },
  {
    "key": "team",
    "type": "tabarray",
    "add": "New",
    "remove": "Delete",
    "style": {
      "remove": "btn-danger"
    },
    "title": "value.name || 'Team '+$index",
    "items": [
        {
        "key": "team[].players",
        "title": "Players",
        "items": [
          {
            "key": "team[].players[].name",
            "title": "Name"
          },
          {
            "key": "team[].players[].age",
            "title": "Age"
          }
    ]
  }
    ]
  },
  {
    "type": "submit",
    "style": "btn-default",
    "title": "OK"
  }
]

and the corresponding schema:

{
  "type": "object",
  "title": "Team",
  "properties": {
    "team": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
            "players": {
              "type": "array",
              "maxItems": 4,
              "items": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "age": {
                    "type": "integer"
                  }
                },
                "required": [
                  "name",
                  "age"
                ]
              }
            }
        },
        "required": [
          "players"
        ]
      }
    }
  }
}

Upvotes: 1

Related Questions