Two ng-repeat loops displaying separate objects arranged by their respective keys

Truth be told, I am at a loss on how to tackle this design challenge.

I have two intricate objects from different sources (simplified):

 var header = {
     fieldName1: {otherstuff: x},
     fieldName2: {otherstuff: y},
     fieldName3: {otherstuff: z}
 }

 var data = [
 {
     fieldName3: {someData: 1},
     fieldName2: {someData: 2},
     fieldName4: {someData: 3}
 },
 {
     fieldName2: {someData: 11},
     fieldName3: {someData: 22},
     fieldName1: {someData: 33}
 }
 ];

My goal is to construct a table with a header row displaying the keys of the first object and rows populated by someData values from the second object.

Naturally, the columns in the table need to match the order of the header rows.

    <table>
      <tr>
        <th ng-repeat="(fieldKey, fieldMeta) in header">{{fieldKey}}: {{fieldMeta.label}}</th>
      </tr>
      <tr ng-repeat="row in data">
        <td ng-repeat="value in row">{{value}}</td>
      </tr>
    </table>

How should I approach this problem? Should I sort the header and data objects first (as objects or arrays), or can I achieve the same using orderBy in the second ng-repeat?

I anticipate coming up with a solution involving around 100 lines of code spread across multiple utility classes if left on my own for a day or two.

While I don't require pre-written code, guidance from seasoned JavaScript developers would be greatly appreciated.

Answer №1

To enhance your code, consider iterating over your header in the second internal ng-repeat, utilizing keys to access the corresponding row data. This way, the order of data in your header will determine the sequence. Keep in mind that this approach may impact performance. Here is an example:

<table>
  <tr>
    <th ng-repeat="(fieldKey, fieldMeta) in header">{{fieldKey}}: {{fieldMeta.label}}</th>
  </tr>
  <tr ng-repeat="row in data">
    <td ng-repeat="(key, meta) in header">{{row[key]}}</td>
  </tr>
</table>

Check out a sample fiddle here. However, bear in mind that relying on object keys for ordering isn't recommended since the order is not guaranteed. Consider converting to arrays of individual objects instead.

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

Unfulfilled expectation of a promise within an array slipping through the cracks of a for loop

I have a function that generates a Promise. Afterward, I have another function that constructs an array of these promises for future utilization. It is important to note that I do not want to execute the promises within the array building function since so ...

Node.JS, Angular.JS, MySql, and Express.JS are four powerful tools commonly used

Creating an application for managing contacts using MySQL, Angular, Node and Express. I am having trouble with editing and updating data in the provided code. Can someone help me complete the EDIT and UPDATE sections? I am new to this, and a significant po ...

Display only the most recent AJAX results

There are times when I encounter a scenario where performing an action on the page triggers an ajax request. If multiple actions of this nature happen in quick succession, each ajax request performs its task (such as updating a list of items) one after t ...

"Utilizing AngularJS for advanced filtering and searching functions, along with

After exploring various solutions on Stack Overflow to identify unique values in angular.js (I am still learning), I am faced with a challenge. My goal is to present these unique values within a select form and use them as filters for data displayed in a t ...

Issue with Axios fetching data with parameter in Next.js not resolving

While working with Next.js, I encountered an issue where the input text value (email) is successfully displayed in the console, but when trying to use this value as a parameter, "{emails}" is being saved in the database instead of the dynamic email value. ...

Is it possible to identify the form triggering the ajax call within a callback function?

There are multiple forms on my website that share the same structure and classes. The objective is to submit form data to the server using the POST method, and display an error message if any issues arise. Here's how the HTML code for the forms look ...

Working with the collapse event in Bootstrap

After purchasing a template from ThemeForest, I found it to be absolutely amazing. However, I am struggling with using Bootstrap as it seems quite complex. The left menu is structured with collapsible ul and multiple li elements. I am seeking assistance o ...

The issue persists with Angular routing as the page does not load even when adding a template, resulting in nothing being

Can someone please help me figure out why I am having trouble routing the app.js file? (function () { "use strict"; var app = angular.module("productManagement", ['ngRoute']); app.config(['$routeProvider', '$locationPr ...

Delete dynamic elements from an array once they are no longer present in the DOM

In this code snippet, I have implemented a button that adds a new object to both the document and an array. When you click on each object, it gets removed from the document. However, the challenge here is how can we also remove it from the array? You can ...

Retrieve a variety of outputs from computed properties in Vue

I have created buttons to select different dates (one for tomorrow and one for next week) with a function that calculates only business days. The 'tomorrow' button should display either 'Tomorrow' or 'Monday', while the ' ...

"Eliminating Redundant JS and CSS Files in Magento to Improve

While analyzing the performance of a website, I noticed that GTmetrix found some CSS & JS files being loaded from different locations but containing the same file. An example: <action method="addItem" module="ves_blockbuilder" ifconfig="ves_blockbuilde ...

Tips for troubleshooting aspx pages following an ajax post back?

I am currently working on a project with a unique architecture. During the page_load event, we utilize a switch case structure: switch (command) { case "LOAD": load(); break; case "UNLOAD": ...

Creating Dynamic Routes and Content in ASP.Net Core and AngularJS Using Database Records

I am exploring the idea of creating a system where the initial URL sub-directory will be dynamic and also serve as a query for the dynamic content displayed on the page. Is this achievable? For instance, users should be able to input the address in the fo ...

Navigate directly to a designated element in a React component without the need to scroll when clicking a link

When viewing a user's profile in React, clicking on an image currently scrolls to that specific image within the entire images page. However, I am looking to modify this behavior so that it navigates directly to the image element without any scrolling ...

Utilizing MongoDb and Node.js for efficient data input

I am currently facing an issue while trying to input data into a mongodb collection using node.js. I believe I have the necessary access to the collection in question. var collection = db.collection("whatsGoingOnEvents"); if(collection){ console.log("hitt ...

Ways to navigate to a different page in React when a user clicks?

When working on my react app, I encountered an issue where the useHistory hook was undefined. How can I troubleshoot this problem and ensure that useHistory is properly defined? App.js import 'bootstrap/dist/css/bootstrap.css' import React f ...

How can you rearrange the order of objects in an array to only include duplicates?

https://i.sstatic.net/XwCDZ.png I don't want to alter the original order of objects in an array. However, I do need to retrieve items in a specific sequence when both the location and place are identical. I attempted a solution but it requires an ad ...

Convert the information within the data variable into a well-formatted JSON structure

I am attempting to transmit JSON information to the server utilizing the following URL: . The data variable is being populated with valid JSON containing properties such as name, email, and URLs. ...

Make sure to transfer any data retrieved from a click event using JavaScript into a different field

Recently I delved into learning JavaScript and stumbled upon a coding problem that has me stumped! I'm looking for a way to display information in the blue area after selecting an option, showing details like the name of the food item and its price. ...

Alter global table data attributes through device detection with Javascript

Initially, I assumed that setting the global font-size of all td's would be a simple task. However, I am having trouble finding a solution. In my existing stylesheet, I have defined the font-size for all td's. td { font-size: 20px; ...