Display most important information from a JSON file

Asked

Viewed 81 times

2

How do I present only some of the most important information on a list such as the following example?

{
    "userId": 1,
    "id": 1,
    "title": "DRF - Delegacia de Roubos e Furtos",
    "endereco": "Praca Maua, 5 - Centro",
    "tel":"2233-2701",
    "long":"-22.896893,0",
    "lat":"-43.181976"
},

It doesn’t matter if you use Angularjs, JSON, Javascript. I’d just like to know how to pull only one piece of information to avoid repetitions. For example: how to present in a location only the name, address and phone number of the location for users to see?

  • 2

    Ok, but how does it pull the information currently? How can we help improve your current code?

2 answers

3


Your question explicitly mentions 'presentation'. So the answer is easy - display only the fields you need. Example:

function SampleController($scope) {
  $scope.dados = [
    {
      "userId": 1,
      "id": 1,
      "title": "DRF - Delegacia de Roubos e Furtos",
      "endereco": "Praca Maua, 5 - Centro",
      "tel":"2233-2701",
      "long":"-22.896893,0",
      "lat":"-43.181976"
    }];
}
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

  </head>
  <body>
    <div ng-controller="SampleController">
      <table>
        <tr>
          <th>
            ID
          </th>
          <th>
            Local
          </th>
        </tr>
        <tr ng-repeat='i in dados'>
          <td>{{i.id}}</td>
          <td>{{i.title}}
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>

When executing this code, note that only two properties are displayed.

2

<html ng-app="app">
<head>CDN ANGULAR JS</head>
<body ng-controller="Crtl">
{{user.id}}
</body>
</html>


angular.module('app', []).controller('Ctrl', function($scope) {
 $scope.user = [
                "userId": 1,
                "id": 1,
                "title": "DRF - Delegacia de Roubos e Furtos",
                "endereco": "Praca Maua, 5 - Centro",
                "tel":"2233-2701",
                "long":"-22.896893,0",
                "lat":"-43.181976"
                ];
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.