Using AngularJS to iterate over an array with a string index using ng-repeat

Is it possible to use ng-repeat to iterate over an array with a string index? Review the sample code snippet below:

The following code is within a controller.

$scope.days = ["mon", "tue", "wed" .... "sun"];
$scope.arr = [];
$scope.arr["mon"] = ["apple","orange"];
$scope.arr["tue"] = ["blue","swish"];
.
.
.
$scope.arr["sun"] = ["pineapple","myfruit","carrot"];

Question - Is there a way to achieve something similar to the following using ng-repeat?

<div ng-repeat="day in days">{{day}}
    <span ng-repeat="item in arr(day)">{{item}}</span> 
    <-- Can we use "arr(day)" like this in Angular -->
</div>

Answer №1

To display items in an array, you can simply use the normal syntax. Take a look at my example on JSFiddle

<div ng-app='app' ng-controller='default' ng-init='init()'>
  <div ng-repeat='day in days'>
    <strong>{{day}}</strong><br/>
    <span ng-repeat="item in arr[day]">{{item}} </span> 
  </div>
</div>

Check out the demo here

Answer №2

To retrieve data from objects or arrays, remember to use square brackets.

<div ng-repeat="month in months">{{month}}
    <span ng-repeat="element in array[month]">{{element}}</span>
</div>

Answer №3

This code snippet demonstrates how to utilize the ng-repeat directive in AngularJS: ng-repeat="item in arr[day]"

Answer №4

Don't forget to include the item number in your printout.

<div ng-repeat="date in dates">
    {{date}}
    <div ng-repeat="(id, name) in itemList[date]">Item #:{{id + 1}} - {{name}}
    </div>
    <br />
</div>

Answer №5

Absolutely, achieving this is completely feasible. Take a look at the successful demonstration provided below:

var app = angular.module("sa", []);

app.controller("FooController", function($scope) {
  $scope.days = ["mon", "tue", "wed", "sun"];
  $scope.arr = [];
  $scope.arr["mon"] = ["apple", "orange"];
  $scope.arr["tue"] = ["blue", "swish"];
  $scope.arr["sun"] = ["pineapple", "myfruit", "carrot"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="sa" ng-controller="FooController">
  <ol>
    <li ng-repeat="day in days">
      <strong>{{day}}</strong>
      <ol>
        <li ng-repeat="item in arr[day]">{{item}}</li>
      </ol>
    </li>
  </ol>
</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

Condition has been fulfilled for both ngShow and ngHide

I am attempting to show a loading icon while data is being loaded, and then display the data once it is ready. However, I am encountering a problem where for a brief moment, both the loading icon and the data are visible... https://i.sstatic.net/vDXOG.png ...

Substitute dynamic Angular expressions with fixed values within a string

Inside my angular controller, I am defining a stringTemplate containing expressions like "{{data.a}} - {{data.b}}". Along with that, I have an object named data with values {a: "example1", b: "example2"}. My goal is to find a way to replace the dynamic ex ...

How do I choose the specific financial date of April 1st in the datepicker selection?

I am trying to set the minimum date as 1st April 2018 and the maximum date as the current date. These are the methods I have already attempted: $("#primarySaleDatePicker").pickadate({ min: new Date(2018, 3, 1), max: 'today', ...

Using React to Retrieve Array Data from an API and Implementing Filtering

I have successfully made a call to my API endpoint and passed the token. However, I only need specific details from the response data - specifically, I want to work with the first index in the array that contains fields. My goal is to fetch all the relevan ...

Sorting with conditions in aggregations

My goal is to implement conditional sorting based on price, where I need to differentiate between two types of prices stored in the database: fixed price and bid. If sellType is fixed price, I want to retrieve the normal price. However, if sellType is bid ...

How to create expandable nodes with lazy-loaded children in Dynatree?

I have successfully implemented a tree navigation menu using Dynatree (). The tree consists of four levels: company, group, user, and computer. Each object within the tree is selectable, opening a page displaying the properties of that specific object. How ...

Ensure the accordion design is adaptable to various screen sizes and orientations, as it is currently only allowing

I'm struggling to achieve responsiveness with the FAQ Accordion on my website. When it initially loads collapsed, the space where it should expand appears blank. I need it to dynamically adjust and create more room when opened up. Additionally, when a ...

Eliminate event listener using unique identifier

Is there a way to retrieve information about all event handlers for an element in JavaScript? $._data($('#element_id')[0], "events"); This will provide a detailed record of every event handler attached to the element. 0: {type: "c ...

Splitting the webpage into two sections with individual scroll bars

My website consists of two elements, a left and right part, both taking up 50% of the screen. <div class="wrapper"> <div class="left"> <div class="box"></div> </div> ...

Issue with importing Typescript and Jquery - $ function not recognized

Currently, I am utilizing TypeScript along with jQuery in my project, however, I keep encountering the following error: Uncaught TypeError: $ is not a function Has anyone come across this issue before? The process involves compiling TypeScript to ES20 ...

I am uncertain about how to interpret this method signature

Can you help me determine the correct method signature for handleError? The linter tslint is indicating an error message that says expected call-signature: 'handleError' to have a typedef (typedef). Here is the code snippet in question: import ...

Disabling caching for Angular AJAX requests in Internet Explorer - a step-by-step guide

My Angular application successfully makes HTTP GET requests to the server when accessed on Chrome and Firefox. However, I recently encountered an issue with Internet Explorer where it caches the GET response. This causes a problem when I need to retrieve u ...

AssertionError [ERR_ASSERTION]: The value of undefined is not equal to 390 in the GitLab

I'm a bit confused about the AssertionError [ERR_ASSERTION]: undefined == 390 in Gitlab. What I need is: The sumSalaries(obj) function, which should take an object obj as a parameter where the field names correspond to the employee's name and t ...

The React render function fails to display the components it is supposed to render

Upon running npm start, the browser opens localhost:3000 but only displays a white page. To troubleshoot, I added a paragraph within the <body> tag in index.html. Surprisingly, the text Hello World! appeared for less than a second before disappearing ...

Using soapUI to extract information from a JSON Response with arrays

After receiving a JSON response from a previous test step in soapUI, I am working on parsing the information using a groovy script. Here's a snippet of the JSON response: { "AccountClosed": false, "AccountId": 86270, "AccountNumber": "2915", ...

What is the storage location for constant arrays in a compiler?

It may seem like a simple question, but I am curious and unable to find an answer. For example, when I type something like the following: int test[1000000] = {0}; Will this array be included in the compiled program code? Or is it just an instruction to r ...

Ensuring website responsiveness beyond just the carousel

I found a carousel (inspired by: https://codepen.io/dobladov/pen/kXAXJx) that fetches images from an API. The carousel will switch images as you click on a different image or use the "previous" or "next" buttons. While there are many examples of putting te ...

The error message indicates that xxx does not function as a constructor

var Fishbowl = require('node-fishbowl'); var fb = new Fishbowl.Fishbowl({ host: 'X.X.X.X', IADescription: 'Reporting Dashboard', IAID: 2286, IANAME: 'node-dashboard', password: 'X', ...

Ways to bring in a Typescript Vue project to a Javascript Vue

I am trying to incorporate this Calendar component into my Javascript Vue 3 project. To achieve this, I have created a new component in my project named ProCalendar.vue and copied the code from the example found in App.vue. Additionally, I have added the n ...

What is the method to set precise values on a range slider?

Currently, I am developing a PHP website that requires a slider for displaying the years of publications. In essence, I need the slider to navigate through the different years when the publications were released. // Database connection and other PHP code ...