Creating a two-dimensional array and transforming it into a table with ng-repeat in Angular

I am looking to create a table using an array with the following structure:

var items = [
    {
        name: 'xxx',
        configurations: [
            { 
                image: 'aaa.jpg'
            },
            { 
                image: 'bbb.jpg'
            },
            ...
        ]
    },
    {
        name: 'yyy',
        configurations: [
            { 
                image: 'ccc.jpg'
            },
            { 
                image: 'ddd.jpg'
            },
            ...
        ]
    },
    ...
]

My goal is to have a single row displaying all images from each element within the array. Each image should be enclosed in its own <td> tags, like so:

<tr>
    <td>
        <img src="aaa.jpg">
    </td>
    <td>
        <img src="bbb.jpg">
    </td>
    <td>
        <img src="ccc.jpg">
    </td>
    <td>
        <img src="ddd.jpg">
    </td>
</tr>

I am using Angular and my current code is as follows:

<tr>
    <td ng-repeat-start="proposition in items">
        <img ng-src="{{::proposition.configurations[0].image}}" />
    </td>
    <td ng-repeat-end>
        <img ng-src="{{::proposition.configurations[1].image}}" />
    </td>
</tr>

However, since the number of configurations within each proposition is dynamic, how can I iterate through them while maintaining the same HTML structure?

Answer №1

If you want to display both items and configurations in AngularJS, you can achieve it by running two ng-repeat loops - one for items and another for configuration.

Alternative Solution

An alternative approach is to flatten your array. This means converting your nested array into a single array with tuples of <image, name>.

You can accomplish this using the map method. Here is a sample code snippet:

 $scope.items =  [
    {
        name: 'xxx',
        configurations: [
            { 
                image: 'yyy.jpg'
            },
            { 
                image: 'yyy2.jpg'
            }
        ]
    },
    {
        name: 'yyy',
        configurations: [
            { 
                image: 'zzz.jpg'
            },
            { 
                image: 'zzz2.jpg'
            }
        ]
    },
];

  $scope.items = $scope.items.map(function(item){
    return item.configurations.map(function(inner){
        return { name: item.name, image: inner.image};
    })
  })

  $scope.items = $scope.items.concat.apply([], $scope.items);

You can find a complete working example here.

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

Difficulty incorporating openId selector into Asp.Net MVC 2

I have been attempting to implement OpenID login functionality on a website using the openid selector JavaScript library. I am following the guidelines provided on this particular site. However, as someone who is not typically a web programmer, I am facing ...

The createReadStream function cannot be found in the uploaded image

I am currently using node v14.17.0, "apollo-server-express": "^2.25.0", "graphql-upload": "^12.0.0" I'm facing an issue with uploading an image as I don't receive the createReadStream from the image that I upload via graphiql. Specifically, I am ...

Creating Production Files for Web using RxJs and TypeScript

I am interested in developing a JavaScript Library using RxJs (5.0.0-Beta.6) and TypeScript (1.8.10). My TypeScript file is successfully compiling. Below are the files I have: MyLib.ts: /// <reference path="../../typings/globals/es6-shim/index.d.ts" ...

Preparing data before using Kendo UI upload function is essential

I need to pass a data (specifically a GUID) to the upload method of the kendoUpload, so that a particular MVC Controller action method can receive this data each time the upload occurs. $("#propertyAttachmentUpload").kendoUpload({ async: { ...

Is there a way to set a value within a jQuery function, and then invoke another function to utilize that value?

Suppose I have a function called isPercentage, which utilizes a value that is defined within the function it's being called in: isPercentage = function(){ if (value < 1){ value = value * 100; console.log(value); ...

Error message: "When using selenium-webdriver in JavaScript, the findElement method for <a> tags cannot be used as a function."&

Seeking the website URL for brand information from this website, I attempted to retrieve it using JavaScript in the console: document.getElementById('phone_number').getElementsByTagName('a')[1].getAttribute('href') However, ...

Leveraging Redux and React to efficiently map state to props, while efficiently managing large volumes of data and efficiently

Utilizing sockets for server listening, the Redux store undergoes continuous updates with a vast amount of data. While updating the store is quick, the process of connecting the state to the component through the redux connect function seems to be slower, ...

Unexpected outcome when converting a while loop to a .forEach statement

Exploring a practical demonstration of sorting an array of objects based on multiple properties, the code implementation involves a while loop (refer to students1 in the snippet below). I attempted to streamline this process by using .forEach, but encounte ...

Flow object with Angular using ng-flow: Existing flow object

Just a quick question that I can't seem to find an answer for on my own or through searching online. Currently, I have a button set up for clicking and uploading files. However, I also want to incorporate drag and drop functionality using the same fra ...

Is there a way to send a Map object to a script file using EJS?

I am facing an issue with passing a Map object to my client-side code. I have successfully used EJS and JSON.stringify(...) in the past for arrays, but it doesn't seem to work for Maps. When I try to console.log(myMap.keys()), I receive the following ...

Displaying Angular charts can enhance the visualization of data by mapping chart labels to the properties of an object

Currently, I am utilizing angularJs Charts which can be found at In my possession is an array of objects that looks something like this: let MyArray = [{a: 1, b: 2,c:3}, {a: 3, b: 4,c:10}, {a: 5, b: 6,c:20}, {a: 7, b: 8,c:30}]; The goal here is to connec ...

Confirming the authenticity of a property within one entity by comparing it to a property within another entity

When validating two objects, sending and receiving countries, it is important to ensure that they are not identical. Specifically, we want to check if the receiving country is the same as the sending country and if it is, return an error message. The fol ...

What is the process of establishing a connection to a web service using Meteor or Node.js?

Currently working on developing a package that interfaces with a web service (either through nodejs or meteor). The web service will be delivering real-time data, so I am in need of a mechanism like a listener or trigger event that can alert me when new da ...

Is it possible for me to utilize jquery and AJAX to invoke a cgi-bin script, and then incorporate a message event to manage Server Sent Event?

I have a cgi-bin program that runs for a long time (3-15 minutes) and I am looking to invoke it using AJAX. While the program is running, I want to receive Server Sent Event data from it and display it on my web page. It's like having a progress monit ...

Is there a way to modify Style Properties in JavaScript by targeting elements with a specific class name using document.getElementsByClassName("Class_Name")?

I am seeking a solution to change the background color of multiple div boxes with the class name "flex-items" using JavaScript. Below is my current code: function changeColor(){ document.getElementsByClassName("flex-items").style.backgroundColor = "bl ...

Using an UnsafeMutablePointer for an array in a C library

I am currently working on integrating a C library into my iOS project, which is developed using the Swift language. One of my functions requires an input parameter where the output values are stored in a typical C double array: double ar[6]; ///... err = ...

Please provide instructions on how to submit a POST request to the API using Restangular

I'm currently utilizing the Django REST framework to write APIs. It functions properly when data is manually entered on this page: http://example.com/en/api/v1/add_comment/ views.py (API) class AddComment(generics.CreateAPIView): """ Creating a new ...

Extract information from a Web Service using Javascript

As a novice in web app development, I am currently working on creating a login page and retrieving user data from a local database using JavaScript. Despite my efforts, I seem to have made an error somewhere in my code. Below is the snippet of my JavaScrip ...

What are the steps to integrate Laravel and AngularJS?

Exploring the integration of Laravel with AngularJS has lead me to ponder on the most effective way to structure such a project. Should I (A) opt for a single domain where an API is consumed directly from the Laravel project, or (B) have website.com and a ...

Bringing in data using .json files in a react native environment with Redux

I have developed a fitness app and I am utilizing Redux to store all the sets and workouts. Currently, I have manually entered all the exercises into Redux data for testing purposes. However, I now have all the exercises stored in a .json file and I want t ...