Looping over object properties using ngFor in an Angular applicationI have a question about looping

Instead of using keyValuePipe or {{data | JSON}}, I am looking for a different solution to write my data. How can I achieve this by using Object.entries, Object.keys, or Object.values?

Check out the code with JSON in Stackblitz here: https://stackblitz.com/edit/angular-ivy-jn8snh?file=src/app/app.component.ts

Answer №1

The structure of your object resembles that of a JavaScript dictionary.

To iterate over the properties of your object, you can use the following method:

<div *ngFor="let property of data">
     <p>{{ property }} - {{ data[property] }}</p>
</div>

Answer №2

Unfortunately, it's not possible to achieve this functionality out of the box. The NgForOf directive in Angular allows for iterating over a collection, but by default, objects do not have the required methods and properties to be iterated over in this way. One potential workaround is to extract the keys of the object using the following code:

<p *ngFor="let property of Object.keys(object)">
  {{property}} {{object[property]}}
</p>

However, due to how ngFor evaluates its contents, the global variable Object is not accessible within its scope. A more effective solution would be to create a custom pipe that transforms your object into an iterable format that meets your specific requirements. It's unclear what exactly these requirements are without further explanation, as simply stating that "keyValuePipe or {{data | JSON}} is not a good solution" doesn't provide enough context or reasoning.

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

What is the best way to capture a screenshot using selenium in conjunction with synchronous JavaScript?

Currently, I am developing an automated test using javaScript and leveraging a node library called webdriver-sync. This library simplifies writing selenium tests by eliminating the need for callbacks and promises, and it utilizes the java Webdriver API. Su ...

Adding a blank data-columns attribute using jQuery:

I am trying to add the data-columns attribute for salvattore.js, but I am facing an issue where I cannot simply add the attribute without providing a value. Currently, my code looks like this: $('#liste-vdl div.view-content').attr("data-columns" ...

Using child routes in eager loaded components can be achieved without making any changes to

In many tutorials, I have noticed that RouterModule.forChild(..) is commonly used for lazy loading. However, I am of the opinion that this forChild method can also be utilized for eager loading. Its advantage lies in the fact that routes can be configured ...

current date validation in time picker

I am looking to implement JavaScript in a time field so that it does not allow the selection of current or past times for the current date. Additionally, I want to restrict the selection of times that are more than 4 hours ahead of the current time on th ...

Running Protractor tests can be frustratingly sluggish and frequently result in timeouts

After spending most of the afternoon struggling with this test, I've tried different approaches but none seem to work. The task at hand is searching for users within the company, generating a table, and selecting the user that matches the name. Curren ...

Functionality of retrieving arrays from PHP via jQuery ajax (JSON) runs smoothly, however encounters issues specifically on web server

No solutions have been able to solve the problem at hand despite finding similar questions. function loadProject(id) { $.ajax({ url: 'loadDrumsetData.php', type: 'GET', data: { i: id }, ...

execute bower install for the specified bower.json file

Let's say my current working directory is c:\foo\ while the script is running. I want to execute bower from there for the c:\foo\bar\bower.json file. This can be done in npm by using npm install --prefix c:\foo\bar. ...

Fabric JS i-text cursor malfunctioning when loading JSON data

When I initially create a fabricjs i-text object in a new window, the cursor works perfectly. However, upon loading a saved JSON file, the cursor no longer functions as expected. I am utilizing the league_gothic font. Please refer to the image below showi ...

Leverage the power of React by utilizing SVGR to easily integrate SVG files

Wondering if there's a way to bring in an SVG file from my public folder and use it as a React component like this: import { ReactComponent as MySvg } from '/assets/svg/mysvg.svg'; const MyComponent = () => { return ( <div> ...

Executing a nested function before moving on to the subsequent code statements

I have a requirement where certain functions in my codebase need to check if a user is logged in before proceeding. Instead of duplicating this check logic, I want to call a single getUser() function each time. Here is the order of operations for the func ...

Executing a function when a user chooses to exit a webpage using the @HostListener('window:beforeunload') method

Utilizing @HostListener('window:beforeunload') allows me to detect when a user navigates away from the page, prompting a dialog window to open. I wish for an event to be triggered or a method to be executed if the user chooses to leave the page. ...

Adjusting Text Width in fabric.js: A How-To Guide

In my experience, I have found that setting textAlign for a fabric.js Text object seems to be ineffective because the textfield's width is automatically adjusted to fit the text perfectly. I searched through the API but couldn't find any way to ...

"Exploring the usual progress of a standard GET request using Axios

My Objective: I am utilizing Vue And Axios, with the goal of displaying the progress in percentage on the console. The Challenge: The request itself takes around 4 seconds or more because it fetches a large amount of data, processes it into an excel fil ...

Error message encountered in Rails Webpacker: "Uncaught TypeError: $(...).tooltip is not recognized as a function

I am working on a Rails 6 application where I compile assets using Webpack 4.39.1 with the help of the Webpacker gem. In my webpack configuration file (config/webpack/environment.js), I have included JQuery 3.4.1 and Popper.js as part of the ProvidePlugin ...

Search through an array of objects and assign a new value

I am facing a challenge with an array of objects structured as shown below: [ { "e_id": "1", "total": 0 }, { "e_id": "3", "total": 0 } ] My objecti ...

Error message: Unable to locate module when using a variable to import an image in React

I've encountered an issue with my React code that I can't seem to figure out. I am integrating the Accuweather API and trying to display the weather icon on my app. Initially, everything seemed to be working fine as I constructed the image path l ...

Is it possible for a MySQL loop to only delete the first entry?

My MySQL looping is not working properly when I click the second button to get their id and continue with the rest of the process. Why is this happening? $(document).ready(function() { $("#deleteSchedule").click(function (e) { e.preventDefault(); ...

How to generate an array within a TypeScript extension function

As I was working on creating an extension method using typeScript, the main goal was to establish a static or normal variable within the method. The ServiceCollector method was invoked three times in order to send and store data or objects in an array. B ...

Using Sequelize's association include results in a null value being returned

I am encountering an issue while attempting to link a table in my query using sequelize-cli. Although my query functions correctly, it fails to populate the Adresse table. Only Patient gets populated, while Adresse array is overlooked and returns null. I ...

What is the best way to display the latitude and longitude values stored in my database on a Google Map interface?

Currently, I have some sample code that calls latitude and longitude values and marks them on maps. However, my goal is to display all latitude and longitude records on the map, not just one. I understand that I need to modify my code to fetch all values ...