A guide on displaying JSON response data in Angular JS with the help of ng-repeat

I am attempting to display the values of a specific JSON in the correct order. Here is how my JSON is structured :

{
"A":[{"id":"21","name":"Andrea"},{"id":"22","name":"Apple"}],
"B":[{"id":"21","name":"Baby"},{"id":"22","name":"Bali"}],
"C":[{"id":"21","name":"Candle"},{"id":"22","name":"Canada"}],
}

How can I display these values using ng-repeat :

<div ng-repeat="item in items">

</div>

For example:

A

Andrea

Apple

B

Baby

Bali

C

Candle

Canada

Answer №1

For a live demonstration, please refer to this example: http://example.com/live-demo

Code snippet

<div ng-repeat="(index, value) in data">
  {{index}}
  <div ng-repeat="item in value">
    {{item.title}}
  </div>
</div>

Answer №2

To loop through the object, you can use the following code:

<div ng-repeat="(k, val) in myData">
   {{k}}
   <div ng-repeat="item in val">
      <div>{{item.title}}</div>
   </div>
</div>

Update: I noticed others have included a jsfiddle link as well so make sure to check it out :)

Answer №3

If you have data that looks like this :

$scope.items={
"A":[{"id":"21","name":"Andrea"},{"id":"22","name":"Apple"}],
"B":[{"id":"21","name":"Baby"},{"id":"22","name":"Bali"}],
"C":[{"id":"21","name":"Candle"},{"id":"22","name":"Canada"}],
}

Imagine each item as a map and iterate through for (key,value) pairs in the map where the value is an associated list. Then, iterate through each list of values as follows:

<div ng-repeat="(key,value) in items">
     {{key}}
     <div ng-repeat="item in value">
       {{item.name}}
     </div>
</div>

Answer №4

Take a look at this example.

Remember to include the key in your ng-repeat loop if you want to access both the key and value of your JSON data.

Here is the code snippet:

<div ng-repeat="(key,value) in data">
   {{key}}
    <div ng-repeat="item in value">
      {{item}}
    </div>
</div>

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

When the canvas is in full screen mode, my div elements are hidden

Currently, I am immersed in a 360-panorama project, utilizing panolens.js and three.js. While Panolens offers fullscreen mode functionality, the problem arises when entering this mode as the canvas conceals all of my div elements. One particular div elemen ...

Encounter a parameter validation error

Just a quick question. I have a JS function that takes a parameter as input. If the passed value happens to be NULL, I want to handle it accordingly. However, my limited experience with JS is making it difficult for me to use the correct syntax. Here' ...

Converting Node JS request to an API into React Fetch syntax

I have encountered a problem while developing an app in React-Native that connects with the Hubspot API. Initially, I tried to make the request using the Node JS request module, but it didn't work well with React Native when Expo was involved. Now, I ...

AngularJS radio buttons are unable to be selected in a simplistic manner

I have implemented a dynamic list display using HTML radio buttons. Here is the code snippet: <html> <head> <script src="angular-v1.5.5.js"></script> </head> <body> <div ng-app="myApp" ng-controller=" ...

What are the steps to update content by referencing an id in the hash URL?

Currently, I am utilizing the following code snippet to extract an ID from the hash URL: var integer = window.location.hash.match(/\d+/) | 0; alert(integer); However, I have encountered an issue where upon using the back button after modifying the U ...

Executing functions across modules in node.js

I need assistance with two of my modules: var client = require('./handlers/client.js'); var server = require('./handlers/server.js'); server.createClient() The client module: var client = function(){ console.log("New client"); ...

Unable to integrate npm package into Nuxt.js, encountering issues with [vue-star-rating] plugin

Just starting with nuxt js and running into issues when trying to add npm packages. Below are my attempts. star-raing.js import Vue from 'vue' import StarsRatings from 'vue-star-rating' Vue.use(StarsRatings) nuxt.config.js plugi ...

Steps to send an object array using a promise

I've put in a lot of effort but haven't found a solution that works well for me. I attempted using promise.all and setting the array globally, but it didn't work out. My goal is to search through three MongoDB collections, match the finds, ...

Is it necessary to include @types/ before each dependency in react native?

I am interested in converting my current react native application to use typescript. The instructions mention uninstalling existing dependencies and adding new ones, like so: yarn add --dev @types/jest @types/react @types/react-native @types/react-test- ...

Trouble arises when attempting to categorize an array of objects in JavaScript

My array consists of various objects: [{id: 1, name: 'Apple', category: 'Fruit'} {id: 2, name: 'Melon', category: 'Fruit'} {id: 3, name: 'iPhone', category: 'Phone'} {id: 4, name: 'Samsung Ga ...

Smooth polygons in a Three.js model

After implementing the following code: var manager = new THREE.LoadingManager(); manager.onProgress = function ( item, loaded, total ) { console.log( item, loaded, total ); }; var texture = new THREE.Texture(); var loader = new THREE.ImageLoader( ...

Limit the length of text using jQuery by specifying the maximum pixel width

Trying to utilize jQuery for a quick function that calculates the pixel width of a string on an HTML page and then shortens the string until it reaches a desired pixel width... Unfortunately, I'm encountering issues with this process as the text is n ...

Converting byte[] to JSON in C# without the need for encoding it to a base64 string

I am looking to convert my model into json format, specifically dealing with a byte[] array that needs to be converted to a base64 string during the process. I do not want the byte[] array converted to a normal string. This is the code snippet I have expe ...

"Watching is not good, so what can we do to improve this situation

In my Angular project, I have been utilizing the $watch function even though it's recommended to avoid doing so. Below is an example of how I've implemented it: $scope.$watch('mysevice.function()', function(tilda) { $scope.valu ...

How to add JSON data into a specific column of a table using

While exploring options for inserting arrays into single database columns, I came across an article that discussed inserting JSON strings. Unfortunately, the article did not provide detailed instructions on how to do this. Despite conducting further resear ...

Managing actions with IconMenu and ListItem in React using MaterialUi

Currently, I am delving into the world of React and attempting to create a simple TODO list using Material-UI. However, I have encountered an issue with handling IconMenu menu actions within a listItem element. I am struggling with triggering the deleteI ...

What is the best way to update the return value of a filter in AngularJS?

My filter relies on the result of the service.show() function: angular.module('util') .filter('formatMoney', ['accounting', 'service', function (accounting, service) { return function (number) { ...

ng-click event triggers twice in directive-driven navigation

I have a directive that I'm using for my navigation, but when both ng-clicks are triggered, they fire twice. I've looked at similar questions here, but can't seem to figure it out. Any help would be appreciated. The HTML element <nav ng ...

Python Scrapy: Extracting live data from dynamic websites

I am attempting to extract data from . The tasks I want to accomplish are as follows: - Choose "Dentist" from the dropdown menu at the top of the page - Click on the search button - Observe that the information at the bottom of the page changes dynamica ...

Encountering a failure when trying to run npm in a React project

I've removed the .lock file and node_modules, then reinstalled, but it's still not working. Can someone assist me with fixing this? npm ERR! gyp ERR! node -v v16.13.0 npm ERR! gyp ERR! node-gyp -v v8.2.0 npm ERR! gyp ERR! not ok npm ERR! node-pr ...