How can I loop through JSON in AngularJS to populate fields without knowing the key value?

Here is the data structure that I'm working with:

{
"0": {
"keyword": "flower",
"short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"pt_value": "5"
},
"1": {
"keyword": "tree",
"short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"pt_value": "8"
},
"2": {
"keyword": "shrub",
"short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"pt_value": "3"
},
"3": {
"keyword": "grass",
"short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"pt_value": "7"
},
"4": {
"keyword": "bush",
"short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"pt_value": "6"
},
"5": {
"keyword": "plant",
"short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"pt_value": "9"
}
}

I want to use ng-repeat in the following way:

<div ng-controller="myController">
    <div ng-repeat="item in items">
        <p>{{ description }}</p>
        <p>{{ value }}</p>
    </div>
</div>

While I know I can do something basic like ng-repeat="product as products," how do I go about iterating through this complex data structure, repeating it, and positioning the content where I need it?

Answer №1

If you're looking to understand how ng-repeat expressions work, consider checking out the documentation on ng-repeat syntax. It's possible to handle repetition using an object.

Let's assume that scope.items represents the object mentioned in the question:

<div ng-controller="myController">
    <div ng-repeat="(k, item) in items">
        <p>{{ item.short_desc}}</p>
        <p>{{ item.pt_value}}</p>
    </div>
</div>

(key, value) in expression – key and value are customizable identifiers, and expression is the collection provided by the scope to iterate through.

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

Guide to exporting several Chart.JS graphs to a PDF document with each graph on a separate page

I am currently utilizing the JavaScript code provided below to generate a pdf file containing multiple charts. However, all the charts are being placed on a single page. My inquiry is regarding how I can customize the code to have each chart on its own sep ...

Incorporating an offset with the I18nPluralPipe

Having trouble with my multiselect dropdown and the text pluralization. I attempted to use the I18nPluralPipe, but can't seem to set an offset of 1. ListItem = [Lion, Tiger, Cat, Fox] Select 1 Item(Tiger) = "Tiger", Select 3 Item(Tiger, Cat, Fox) = ...

How come the pop-up isn't displaying in the middle of the screen?

I have encountered a positioning issue while using a semantic modal with Angular. The problem arises when I try to display the modal as it does not appear in the correct position. Below is the code snippet from my HTML file: <div class="ui modal"> ...

Ways to resolve the issue of data not displaying on the page, even though it appears in the

I am attempting to upload an image that has been converted to a URL using Ajax. The issue I am facing is that $image = $_POST['imgData']; does not display anything, but when I check the developer tools in the network preview, the data can be seen ...

Unusual actions from the jQuery Steps extension

I am currently utilizing the jQuery Steps plugin (FIND IT HERE). The issue I'm facing lies within an IF statement that is causing the wizard to return to the first step instead of staying on the current indexed step. While all other IF statements are ...

Which is the optimal approach for utilizing REST API and parsing data on Android - Jackson or Groovy?

Hello there! I'm looking to download data and parse it within a textview. Can anyone recommend the best tool for this task? To my knowledge, groovy does not require a model class whereas jackson functions similar to Gson with the use of a model class. ...

Stop capturing mouse and keyboard events within a specific div element on all web browsers

I manage a website with forms that have jquery autosave features and are updated using ajax. These forms include various types of input elements such as textboxes, jQuery UI datepickers, and time pickers... <div id="content"> <form id="line1"&g ...

Searching for $or command in express.js

Is it possible to use $or in the app.get() function of an express.js application? For example, how would I write a find() query that includes $or like this: db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } ) I'm curious about ...

Attempting to grasp the process of converting CSV files into JSON format using Python

import csv import json import sys csv_file = open('a1.csv','rU') fields = ["bacteria", "Penicillin", "Streptomycin", "Neomycin", "Gram_Staining"] csv_reader = csv.DictReader(csv_file, fields) json_file = open('json_out.json' ...

Retrieving data with jSON through the local storage API

Seeking assistance with a problem I'm facing. Being new to this, the textbook Headfirst into programming isn't very helpful in explaining. After researching on stackoverflows, I'm still struggling. Any guidance would be greatly appreciated. ...

Tallying the number of keys within the JSON

Here is an example of a JSON object: var json = { "pages": [{ "name": "page1", "elements": [{ "type": "text", "name": "question1" }, { "type": "text", ...

No location matched any routes

I'm currently working on a notes application, and I've encountered an error when trying to edit the notes. The error message says "No routes matched location id ...". Any idea what could be causing this issue? The approach I'm taking is to ...

Angular allows for the addition of unique styles to a JSON

When I receive a JSON response from BE, it is displayed as is. However, I would like certain keys such as "type, includeDeactive, code etc..." to be in bold and the corresponding values to be italicized after the colons (:). Example: body: { ...

Choosing a single random key-value pair from a specific attribute within a JSON schema

Is there a way to generate a new JSON schema based on the existing one, but with only one key-value pair chosen randomly from the "properties" attribute? The new schema should retain the "title" and "type" attributes as well. { "title": "animals object" ...

Transferring user-selected values from JavaScript to a PHP file

When sending values from JavaScript to a PHP file, everything works smoothly when all the values are collected. Step1 functions perfectly as both fields are mandatory. However, in Step2, values are only sent when all the fields are selected. There are co ...

Changes to the parent state will not be reflected in the child props

When the child component PlaylistSpotify updates the state localPlaylist of its parent, I encounter an issue where the props in PlaylistSpotify do not update with the new results. I've been struggling to figure out what I'm missing or doing wrong ...

Chrome PWA debugger fails to respond when attempting to use the "add to homescreen" button

I have been endeavoring to implement the "add to Homescreen" prompt feature on my website. After thoroughly reviewing Google developer documentation, I have successfully configured everything accordingly. However, when I try to manually add my page to the ...

Does AngularJS really allow for seamless event communication between modules?

I am in search of a solution to effectively send an event from one Angular module to another. After browsing through various resources, I stumbled upon a thread that perfectly outlines the solution I had in mind. The thread titled How to send message from ...

Tips for managing and loading data into a dataGrid or table with the help of ReactJS and ReactHooks

Struggling to retrieve user input data from the form and display it in the table/Datagrid below, without success. Follow the process flow outlined below Once the user submits the form and clicks the send now button, the {handleSubmit} function is trigger ...

Generate a new perspective by incorporating two distinct arrays

I have two arrays containing class information. The first array includes classId and className: classes = [ {classid : 1 , classname:"class1"},{classid : 2 , classname:"class2"},{classid : 3 , classname:"class3"}] The secon ...