How to loop through object properties in AngularJS using ng-repeat

I am working with an object structured like this:

Obj = {
  "elements":[
               {"name":"something","id":"v1-234"},
               {"name":"somethingElse","id":"v1-239"}
             ],
  "paging":{
    "next" : "100",
    "total": "2000"  
   },
 "linked"={
            "partners":[
                         {"id":"82","name":"A"},
                         {"id":"83","name":"B"}
                       ],
             "instructors":
                       [
                         {"id":"11232","name":"alex"},
                         {"id":"11432","name":"boob"}
                       ]
           }
    }

The elements array, as well as the partners and instructors arrays within the linked object, contain an equal number of items. I have only included 2 items for demonstration purposes, but there could be more.

In this structure, Obj.elements[0] corresponds to Obj.linked.partners[0] and Obj.linked.instructors[0]. This pattern continues for all items in the arrays.

I am wondering how I can use ng-repeat to display

Obj.elements[i], Obj.linked.partners[i], and Obj.linked.instructors[i]

simultaneously in the HTML template?

Answer №1

If you always have arrays of elements, partners, and instructors that are the same length, and you want to combine their names together, you can use the following approach.

<div ng-repeat="element in Obj.elements">
    {{element.name}} {{Obj.linked.partners[$index].name}} {{Obj.linked.instructors[$index].name}}
</div>

The $index variable is utilized by Angular in a ng-repeat loop to keep track of the position while iterating through an object. You can find more information in the documentation: ng-repeat

Answer №2

ng-repeat introduces the $index variable:

<ANY ng-repeat="element in model.elements">
   <span ng-bind="element.name"></span>
   <span ng-bind="model.linked.partners[$index].name"></span>
   <span ng-bind="model.linked.instructors[$index].name"></span>
</ANY>

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

Automatically trigger a Bootstrap 5.2 modal when the page loads

Currently, I've been utilizing Bootstrap in conjunction with JQuery and have a specific code snippet that displays a Modal when a page is loaded. However, with the latest version 5.2 of Bootstrap, there is a push to move away from using JQuery (which ...

Improving Performance When Handling Multiple Events with Socket.io

Exploring socket.io event handling. Which is the more effective approach: socket.on('message', function (message) { if(message.message1) { // perform action } else if (message.message2) { // take alternative action } else ...

Having trouble with tabs in jQuery?

I'm having trouble setting up tabs in a reservation form with 3 tabs that include text boxes for user input. I can't seem to get it working properly and I'm not sure where I've gone wrong. Could it be due to the placement of the content ...

Material UI DateTimePicker Displaying Incorrectly

I am implementing a new Material UI date time picker on page load by setting the open prop. <Grid item xs={6} className={styles.CampaignDates_calendar_right}> <MuiPickersUtilsProvider utils={DateFnsUtils} className={styles.CampaignDates_calendar ...

Having trouble with SCSS styles not being applied after refactoring to SCSS modules?

Currently, I am in the process of restructuring an application to ensure that component styles are separated from global styles using CSS modules. However, I have come across an issue where the styles are not being applied correctly. The original code sni ...

Is there a way to run Javascript using an ExtJS AJAX request without using eval() like how it can be done with JQuery?

I need to run JavaScript on pages loaded via Ext.Ajax.request. In order to achieve this, I must load the scripts and execute them using eval(), as shown below: Ext.Ajax.request({ url: 'content/view_application.php', success: function(obj ...

Preventing Shift: How to Only Replace the Chosen Vue Draggable Item Without Affecting Other Grid Items

Let's try an example to demonstrate: https://codesandbox.io/s/j4vn761455?file=/src/App.vue:112-116 Step 1: Drag item 4 to where 5 is. Notice how 4 and 5 switch positions. Step 2: Now, drag item 1 to position 6. Watch as 1 moves to where 6 was, pushi ...

Tips for sending values via props to a different component and the common error message: "Consider using the defaultValue or value props instead of setting selected on the <option> element"

Currently, I am attempting to pass the selected value using the prop: handle state change, but encountering two errors. Error 1 : Instead of setting selected on <option>, use the defaultValue or value props. Error 2 : A property 'active' ...

Are there any testing frameworks available for JavaScript that are similar to Junit and specifically designed for testing object-oriented JavaScript within Node

What are some recommended methods for testing object-oriented JavaScript in Node.js? For instance, let's consider the following Cat.js class: function Cat(age, name) { this.name = name || null; this.age = age || null; } Cat.prototype.getAge ...

What is the process for implementing a Content Security Policy to enable the loading of external JS files from a CDN in ExpressJS?

When working with ExpressJS, the HTML file is loaded in the following manner: app.use(express.static(__dirname + '/src/templates/')); Within the HTML file, here is an example of a meta tag containing Content Security Policy: <meta http-equiv= ...

Having trouble with Next Js and Typescript, specifically receiving the error TS2304: Unable to locate name 'AppProps'?

I encountered the error message "TS2304: Cannot find name 'AppProps' when trying to launch a basic Next Js project using TypeScript. After searching on Stack Overflow for similar issues, I haven't found any solutions that work for me. imp ...

Adjust the width of menu options using jQuery

One issue I am facing is with the menu on my homepage. I would like the menu options to enlarge upon hover, and while I have achieved this effect, there is a flaw in the animation: When I move my cursor away before the animation completes, the option abru ...

Switching Formview mode using javascript

Currently, I have a formview on my website and I am looking to change the formview mode using JavaScript. Specifically, I want the formview to switch between insert mode and edit mode based on different interactions with buttons. I have a repeater on my p ...

Should instance variables be imported into Angular from Ruby on Rails? If so, what is the best way to do it?

I am currently developing a web application that utilizes Angular on the client side and Ruby on Rails on the backend. The app will be displaying a dynamically generated list of articles. One thing I'm considering is the typical model of interactions ...

Ember Component Incorporating Keyboard Input

I recently developed an ember component that features a rectangular block in a green canvas. Everything is working smoothly so far. However, I am facing some challenges with implementing keyboard input commands (A S D W) to navigate the rectangle within t ...

Angular JS appears to be causing the DOM to freeze up while utilizing the ng-repeat directive to loop through

I have a current app where clicking a button triggers an $http request to fetch and return some data. The retrieved information is then used to update the $scope variables rows and columns, which are then looped through using ng-repeat. However, I've ...

Executing a Javascript function when a form is submitted

function validateForm() { alert("Form submitted successfully"); return true; } <form onsubmit="return validateForm()" action="add.php" method="post" id="p1"> <center>Add New Survey</center> <p>&nbsp;&l ...

Text Parallax Effect

For my website, I am interested in incorporating a unique parallax effect. Instead of just fixing a background image and allowing scrolling over it, I want to apply this effect to all of the content on my site. The website consists of a single page with m ...

Synchronization-free API and callback functions

I am in need of utilizing an asynchronous service. My current approach involves sending data to this service using PHP and CURL, as well as receiving data from a URL provided by the service. How can I effectively respond or wait for feedback from this serv ...

Leveraging ng-repeat with multiple arrays

I'm currently developing a weather application, but I've hit a roadblock when trying to tackle what I thought would be a simple task. Unfortunately, my limited experience with Angular has made it difficult for me to figure out how to achieve this ...