Having difficulty retrieving the correct value for 'key' from JSON in AngularJS

I am aiming to create a table using AngularJS based on the nested JSON data provided below. The desired table format is outlined as follows:

{
  "traits": ["Number of Observations","Observed Number of Exceptions","95-99", "95-95","99-95", "99-99"],
  "values": [
    {
      "AAAA1": {
        "Number of Observations": 252,
        "95-95": {
            "Test Statistic": -1.040531963428947,
            "P-Value": 0.85095358899764695,
            "Test Outcome": "p-value >=0.05"
        },

        "95-99": {
            "Test Statistic": 5.368809379876272,
            "P-Value": 3.9629067916102656e-08,
            "Test Outcome": "p-value <0.01"
        },

        "Observed Number of Exceptions": 9
      }
    },
    {
      "AAAA2": {
        "Number of Observations": 43,
       "95-99": {
            "Test Statistic": -1.040531963428947,
            "P-Value": 0.85095358899764695,
            "Test Outcome": "p-value >=0.05"
        },
        "95-95": {
            "Test Statistic": -0.46245865041286727,
            "P-Value": 0.67812377583172401,
            "Test Outcome": "p-value >=0.05"
        },

        "Observed Number of Exceptions": 7
      }
    }
  ]
}

https://i.sstatic.net/U0NZ9.png

Under the 'values' key, there are two objects that will translate into two rows in the table. My issue lies with looping through objects like '95-95', '95-99', etc., and generating three columns for each object. Currently, I have managed to create three columns under the '95-95' object; however, I hard-coded the 'AAAA1' key value in ng-repeat. I am struggling to dynamically access the middle-level Object key value not within any ng-repeat. Is there a way to achieve this?

<tr ng-repeat="(key,value) in Controller.values">

    <td class="text-center"  ng-repeat="(key1,value1) in value['AAAA1']['95-95']">
        {{value1}}                   
    </td>

Due to this challenge, I am currently relying on the static code below which does not serve my needs effectively.

<td class="text-center"  ng-repeat="(key1,value1) in value">
       {{value1["95-95"]["Test Statistic"]}}                 
</td>

<td class="text-center" ng-repeat="(key1,value1) in value"> 
    {{value1["95-95"]["P-Value"]}}                   
</td>

<td class="text-center" ng-repeat="(key1,value1) in value">
    {{value1["95-95"]["Test Outcome"]}} 
</td>

Answer №1

It is essential for 'AAAA1':'95-95' to be formatted as an Array.

Unfortunately, ng-Repeat can only function with arrays.

Answer №2

If you wish to avoid including a directive or code within the controller, you can achieve it in the following manner:

const myApp = angular.module('myApp', []);

myApp.controller('Ctrl', ($scope) => {
$scope.values = 
  [
    {
      "AAAA1": {
        "Number of Observations": 252,
        "95-95": {
            "Test Statistic": -1.040531963428947,
            "P-Value": 0.85095358899764695,
            "Test Outcome": "p-value >=0.05"
        },

        "95-99": {
            "Test Statistic": 5.368809379876272,
            "P-Value": 3.9629067916102656e-08,
            "Test Outcome": "p-value <0.01"
        },

        "Observed Number of Exceptions": 9
      }
    },
    {
      "AAAA2": {
        "Number of Observations": 43,
       "95-99": {
            "Test Statistic": -1.040531963428947,
            "P-Value": 0.85095358899764695,
            "Test Outcome": "p-value >=0.05"
        },
        "95-95": {
            "Test Statistic": -0.46245865041286727,
            "P-Value": 0.67812377583172401,
            "Test Outcome": "p-value >=0.05"
        },

        "Observed Number of Exceptions": 7
      }
    }
  
  ]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<body ng-app="myApp">
  <table ng-controller="Ctrl">
    <tbody ng-repeat="value in values">
      <tr ng-repeat="(name, objectAA) in value">
        <th ng-bind="name"></th>
        <td ng-repeat="(key, value) in objectAA['95-95']" ng-bind="value">
          
        </td>
        <td ng-repeat="(key, value) in objectAA['95-99']" ng-bind="value">
          
        </td>
      </tr>
    </tbody>
  </table>
</body>

To eliminate the '95-95' and '95-99' specifics from the HTML, you can extract them within the controller.

By implementing specific code in the controller, the process can be achieved as shown below:

const myApp = angular.module('myApp', []);

myApp.controller('Ctrl', ($scope) => {

  $scope.rows = []

$scope.values = 
  [
    {
      "AAAA1": {
        "Number of Observations": 252,
        "95-95": {
            "Test Statistic": -1.040531963428947,
            "P-Value": 0.85095358899764695,
            "Test Outcome": "p-value >=0.05"
        },

        "95-99": {
            "Test Statistic": 5.368809379876272,
            "P-Value": 3.9629067916102656e-08,
            "Test Outcome": "p-value <0.01"
        },

        "Observed Number of Exceptions": 9
      }
    },
    {
      "AAAA2": {
        "Number of Observations": 43,
       "95-99": {
            "Test Statistic": -1.040531963428947,
            "P-Value": 0.85095358899764695,
            "Test Outcome": "p-value >=0.05"
        },
        "95-95": {
            "Test Statistic": -0.46245865041286727,
            "P-Value": 0.67812377583172401,
            "Test Outcome": "p-value >=0.05"
        },

        "Observed Number of Exceptions": 7
      }
    }
  
  ]
  
  $scope.values.forEach((value) => {
    Object.keys(value).forEach((name) => {
      $scope.rows.push({name: name, value: value[name]});
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<body ng-app="myApp">
  <table ng-controller="Ctrl">
      <tr ng-repeat="row in rows">
        <th ng-bind="row.name"></th>
        <td ng-repeat="(key, value) in row.value['95-95']" ng-bind="value">
          
        </td>
        <td ng-repeat="(key, value) in row.value['95-99']" ng-bind="value">
          
        </td>
      </tr>
  </table>
</head>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered on PUT request: Headers cannot be set after they have already been sent

Every time I attempt a PUT request on the specific route, I encounter the error message "Can't set headers after they are sent". Despite receiving a 200 response, the data is not being updated in the database. Can anyone help me identify the issue her ...

Tips for bringing in and taking out data in Vue

I have a set of files called total.vue and completeness.vue. I aim to display the chart from total.vue within completeness.vue, which is my designated admin dashboard where I plan to feature 2 or 3 additional charts. For this task, I will be exporting con ...

Emphasizing the date variable within a spreadsheet

Hey there! I'm struggling with the code below: <html> <head> <title>highlight date</title> <style> .row { background-color: Yellow; color:blue; } </style> <script type="text/javascript"> </script> &l ...

Tips for implementing Google Captcha on 2 Django Forms within a single page

My website is being targeted by bots, so I need to implement a captcha to prevent them from submitting forms. Currently, I have both a login and register Django form on a single page. The issue with my current setup is that the captcha is placed under ea ...

Problematic rendering of JSON decoded array in HTML display

I am currently working with a database that contains data encoded in JSON format. My goal is to display this data in an HTML table, but I encountered an issue when trying to handle an array within the JSON cell. The output in the HTML table shows the word ...

Trouble with fetching value from function in Angular 2

I have successfully implemented musale/angular2-stripe. Everything is functioning well, but I am encountering an issue extracting a value from the component. openCheckout() { this.disabled = true; var handler = (<any>window).StripeCheckou ...

Prevent the onClick event from being triggered by utilizing JSON data

I am trying to implement a feature where a button click is disabled based on a parameter fetched from a JSON file: JSON Parameter "reactJson": { "disable:"true" } Currently, my onClick method is functioning correctly. However, ...

In React JS, when the navbar button is clicked, the URL is updated but the content does not refresh

Upon clicking the button, the URL changes but the content remains the same. Interestingly, if I use an <a> tag instead of a <Link> tag, the page loads and the content updates as well. However, when using the <Link> tag, the data does not ...

Transferring data from JavaScript variables to PHP function through AJAX requests

Within my dashboard.php, there is a JavaScript function triggered by a button click event named getTeamMembers. This function receives values from the user's interaction and passes them to a PHP function also present in dashboard.php. Despite my effo ...

Exploring the depths of futbin.com through web scraping

I am in the process of gathering a dataset containing time series data for FIFA ultimate team players from futbin.com. After some research, I came across a script on GitHub https://github.com/darkyin87/futbin-scraper which has the capability to extract the ...

Tips for setting up a select dropdown in AngularJS using ng-options when the value is an object

How can I set the initial selection of a select field using ng-options? Consider the items we have: $scope.items = [{ id: 1, label: 'aLabel', subItem: { name: 'aSubItem' } }, { id: 2, label: 'bLabel', subItem: { ...

WebDriver integration with the AngularJS form

In the HTML code, there is a file upload form with the data type input[type="text" i]. I am trying to select and upload a file without using the Robot class or KeyEvent. The class in the HTML looks like this: class="form-control ng-pristine ng-invalid ng-t ...

Having trouble with jQuery modal not adjusting its height properly

I am a jquery modal popup newcomer. I can open the modal when clicking on the hyperlink, but I'm having trouble with the height setting. Despite my attempts and searches online, I couldn't figure it out. When trying to post a question about the & ...

Troubleshooting problems with looping in Chart.js using PHP and JSON

I'm in the process of designing a dashboard to display data sourced from a database. Currently, I am utilizing a separate JSON encoded file called "data.php" for this purpose. A jQuery Ajax request is being used to fetch this data and populate a Chart ...

Utilizing jquery to conceal a table row with unselected input - a guide

I need help with creating a filter that, when activated by a button press, will display only the rows in the table that have a checked input. <table class="table"> <tr> <th>Name</th> <th>InUse</th> ...

What is the best way to eliminate an element from a state using the useState hook?

I need help with generating a list of values from checkboxes. const [checkedState, setCheckedState] = useState({}); const handleOnChange = (e) => { if(e.target.checked === true){ setCheckedState({ ...checkedState, [e.target.name]: e.target.checke ...

Changing the event when a class is active in Vue3

Question I am looking for a way to trigger an event when the 'active' class is added to an element. How can I achieve this? I believe this could potentially be accomplished using a watcher method, but I am unsure how to watch for the applicatio ...

Deactivate additional fields when choosing an option from the drop-down selection menu

When designing a form with a select dropdown that displays various options, I encountered an issue. I want to disable certain fields if a specific option is chosen from the dropdown. For instance, if "Within Company" is selected in the transaction type, I ...

Angular.js controller unable to receive object array from $http.get in factory

I am fairly new to Angular development and I've hit a roadblock that's been holding me back for a while. My factory is successfully creating an array filled with objects, specifically RSS feed data, and I can see it loading in the console. Howev ...

observe unique attributes of various objects' keys

I am working with a specific object in my codebase that looks like this: vm.obj = { prop1: 1, prop2: 'test2', prop3: ['test 3'], hashes: { objToWatchOnePlusHash123: { watchThis: 'value 1', ...