Showing Nested Arrays in Angular 2

If I have an array with image links as shown below, how can I display them in HTML?

array = [
{
  img: [
        {0: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
        {1: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
      ],
}
{
  img: [
        {0: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
        {1: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
      ],
}
]

I understand that if the structure was simpler like img : 'url', then the HTML code would look like this:

this.imgs = this.myprovider.getArray();

HTML :

<div ngfor="let item of imgs">{{item.img}}</div>

Answer №1

When dealing with an array of img objects where the key is a number starting at 0, you can simply use the loop index to retrieve the value:

<ul>
  <li *ngFor="let item of array">
    <ul>
      <li *ngFor="let image of item.img; let i = index">
        <img src="{{ image[i] }}" />
      </li>
    </ul>
  </li>
</ul>

Although Stack Blitz may have trouble loading the image URLs, the provided output demonstrates that the HTML structure is correct: https://stackblitz.com/edit/nested-array-example

Answer №2

Give it a shot

<div ngfor="let picture of pictures">{{picture.picture.toString()}}</div>

or

<div ngfor="let picture of pictures">
    <div ngfor="let pic of picture.picture">{{pic}}</div>
</div>

Answer №3

To achieve nested iteration, you can loop through your data twice in the following manner:

<ng-container ngfor="let childImages of imgs">
    <div ngfor="let item of childImages">{{item.img}}</div>
<ng-container>

It's important to use <ng-container> instead of any other HTML element to maintain the appearance and functionality of your HTML code.

Answer №4

<div *ngFor="let element of pictures">
    <div *ngFor="let image of element.picturesArray; let j=index;">{{image[j]}}</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

Developing various VueJS TypeScript projects utilizing a shared library

In the process of developing two VueJS applications using TypeScript, I have created one for public use and another as an admin tool exclusively for my own use. Both applications are being built and tested using vue-cli with a simple npm run serve command. ...

Manipulate data in a component with Angular's behavior subject, then reflect these changes in a separate component

In my app, I have two components that are not related to each other and one service available. When a user clicks on comp1, I want to set a Boolean value to true in both comp1 and comp2. However, when I tried using behavior subject to achieve this, the c ...

There is no 'Access-Control-Allow-Origin' header found on the requested resource in Heroku for Node.js

Here is the header setup in my Node.js API app: res.header("Access-Control-Allow-Origin", "*"); res.header( "Access-Control-Allow-Headers", "Origin, X-Requested, Content-Type, Accept Authorization" ); ...

What is the necessity of utilizing getStaticPaths() alongside getStaticProps()?

When working with dynamic routes, such as [id].js, the static pages generated using npm run build will result in an [id].html page. Any route containing /something will display "Hello World". However, when we dynamically generate content on the page by ut ...

Leverage the power of Webpack to make Vue available globally

Currently, I am dealing with a situation in a legacy Rails application that has undergone some migration to integrate Webpacker and Vue. Additionally, we are utilizing a legacy script loaded through a CDN that also requires Vue. However, instead of bundlin ...

What is the process for uploading a text file into JavaScript?

I'm currently facing an issue with importing a text file from my local computer into JavaScript in order to populate HTML dropdowns based on the content of the file. Despite spending considerable time searching for solutions on stack overflow, I haven ...

Generating instances using TypeScript generics

Looking to create a factory for instantiating classes with generics. After checking out the TypeScript docs, everything seems to work as expected. Here's a simplified version of how it can be done: class Person { firstName = 'John'; ...

Deprecated message received from the body-parser node module

Currently learning Node.js, I've been utilizing the 'express' framework and installed body-parser successfully. However, upon starting my app, I encountered this message from Node: body-parser deprecated bodyParser: use individual json/urle ...

Vuetify: Utilizing condition-based breakpoints

Here is the layout that I am working with: https://i.stack.imgur.com/qlm60.png This is the code snippet that I have implemented: <template> <v-card> <v-card-text> <v-container grid-list-xl fluid class="py-0 m ...

Struggling to differentiate between the various CSS elements on a webpage and pinpointing the correct CSS for use

I have been attempting to replicate the "card-deck" layout that showcases eight cards on a specific webpage. After successfully copying the HTML structure by inspecting the card-deck class, I am now facing the challenge of locating the corresponding CSS ...

JavaScript function to divide arrays into sequences

I have an array that looks like this: var arr = [12, 13, 14, 17, 18, 19, 20] I'm trying to figure out how to iterate through this array and split it into two arrays based on sequences. Essentially, if i+1 != true, I want to create a new array. var ...

Error: The property you are trying to set is undefined and cannot

When I attempt to set a property 'error' that is undefined, I receive a TypeError. The problematic line of code looks like this: this.error = error.code; This issue arises in an Angular Reactive Form while making a call to a web service. Below i ...

Modifying the default text within a select box using jQuery

Within a select box identified as 'edit-field-service-line-tid', there is default text displayed as '-Any-'. This particular select field has been generated by Drupal. I am looking to use jQuery to change the text '-Any-' to ...

Is there a way for me to import a variable from one file and use require() to access it in another file?

This is my current folder structure: collegesapp -- |-- node_modules -- express | -- connect | -- jade | -- passport |-- routes -- routes.js ...

Altering Hues with a Click

One thing I wanted to achieve was changing the color of a hyperlink once it's clicked. I managed to make it work by using the code below: var current = "home"; function home() { current = "home"; update2(); } function comp() { current ...

What is the best way to send an email with a randomly generated HTML output using a <button>?

I am currently working on a feature where a random HTML output is sent to me via email whenever a user clicks on a button on the website page. The user receives a code when they click the button, and I want that code to be emailed to my address automatical ...

Restricting JQuery pop-up to be shown only once

I'm looking for a solution to have a pop-up appear only once when the mouse leaves the screen. I'm not very experienced with coding, so I'm struggling to figure out how to achieve this. // Implementing Exit Intent function addEvent(obj, ev ...

What do you understand by the term "cyclic data structures"?

According to the information found on the JSON site, it states: JSON does not allow for cyclic data structures, hence one must avoid passing cyclical structures to the JSON stringify function. I am curious about what this means. Could someone provide a ...

Displaying buttons based on the existence of a token in Angular - A guide

Can you assist me with a coding issue I'm facing? I have implemented three methods: login, logout, and isAuthenticated. My goal is to securely store the token in localStorage upon login, and display only the Logout button when authenticated. However, ...

Retrieve the title of the page that triggers the loading of another page through JQuery

My ASP.NET/C# page utilizes jQuery to load a Navigation.aspx page which includes my header and navigation buttons: <script> $(function(){ $("#nav-placeholder").load("Navigation.aspx"); }); </script> Within t ...