Issue: The program is unable to locate a suitable object 'John' to support the operation. NgFor is only compatible with binding to collections like arrays and iterables

I am dealing with a JSON object that I need to display in a table. Here is my JSON data:

json: string = `{
          "name" : "John",
          "surname" : "Walsh",
          "age" : "23"
    }`;

To present this data in a table, I attempted the following code snippet:

<table width="700">
            <caption>All Users</caption>
            <thead>
                <tr>
                    <th>name</th>
                    <th>surname</th>
                    <th>age</th>
                </tr>
            </thead>
            <tbody>
                 <tr>
                    <td *ngFor="let names of users"></td>
                       {{ names}}
                </tr>
            </tbody>
        </table>

However, when I tried to parse the JSON data using JSON.parse(), I encountered an error message:

Error: Cannot find a differ supporting object '[object Object]' of type 'John'. NgFor only supports binding to Iterables such as Arrays.

In an attempt to resolve this issue, I converted the JSON object into an array using the following code snippet:

arr:Array<{key: string, value: string}> = [];
constructor(){
    for (var key in this.users) {
      if (this.users.hasOwnProperty(key)) {
       this.arr.push({ key: key, value: this.users[key] });
      }
 }
}

Despite this conversion, I still faced difficulty in iterating over the array using NgFor.

Answer â„–1

First and foremost, it is essential to rectify the array of your users in the following manner:

users = [{
      "name" : "John",
      "surname" : "Walsh",
      "age" : "23"
},{
      "name" : "Mike",
      "surname" : "Mikic",
      "age" : "25"
}];

Simultaneously, you need to loop through and generate a new row each time you pass one object:

<table width="700">
    <caption>All Users</caption>
    <thead>
        <tr>
            <th>name</th>
            <th>surname</th>
            <th>age</th>
        </tr>
    </thead>
    <tbody>
         <tr *ngFor="let user of users">
            <td>
                  {{user.name}}
            </td>
            <td>
                  {{user.surname}}
            </td>
            <td>
                  {{user.age}}
            </td>
        </tr>
    </tbody>
</table>

Answer â„–2

<div *ngFor="let person of employees">
    <p > {{ person.name}}</p>
    <p > {{ person.jobTitle}}</p>
    <p > {{ person.department}}</p>          
</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

Tips for incorporating Response_type id_token+code into a login URL within an Angular application through the MSAdal library

We are currently utilizing the msadal library for Azure ad B2C login. I am now looking to include the response_type in the URL https://login.microsoftonline.com/common/oauth2/authorize when a user logs in. Here is the code being used for login: if (!this ...

Observable in RXJS, with a pipe that transforms based on a dynamic function

I want to create an observable that gets populated with a custom pipe every time it is called. Let me explain this concept with an example: const myObservable = timer(1000); return myObservable.pipe(getCustomPipe()); function getCustomPipe() { return c ...

Executing a cloud function in Firebase from an Angular-Ionic application by making an HTTP request

I am a newcomer to GCP and app development, so please bear with me if this question seems mundane. Currently, I have an angular-ionic app that is connected to Firebase allowing me to interact with the Firestore database. Now, my challenge is to invoke a ht ...

Tips for altering the hue of an inline svg within Angular?

I'm not very familiar with SVGs. Currently, I am using ng-inline-svg package to display inline SVG images. Is there a way to change the color of the SVG image? I've looked on stack overflow for solutions but haven't found anything that wor ...

Is it possible to alter the meaning of a word using the ngIf condition?

As a newcomer to Angular and Ionic, I am experimenting with retrieving JSON data from a URL and translating the words received to another language. My initial attempt using ngif did not yield the desired result! This is what I tried to do in order to chan ...

Display the dynamic change of minutes and seconds every second on an Ionic HTML page

I created a JavaScript countdown counter that displays minutes and seconds using the following line of code: document.getElementById("demo").innerHTML = Number(this.minutes) + 'm' + Number(this.seconds) + 's '; However, on iOS devices ...

Issue with AWS SDK client-S3 upload: Chrome freezes after reaching 8 GB upload limit

Whenever I try to upload a 17 GB file from my browser, Chrome crashes after reaching 8 GB due to memory exhaustion. import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3'; import { Progress, Upload } from "@aws-sdk/lib-storage& ...

Pattern for defining objects with indexes in Typescript

My data is structured as shown below: let equipment:Equipment = { "1": { "name": "abc", "data": 123 }, "2": { "name": "def", "data": 234 }, "3": { "name": "ghi", "data": 345 }, ... } Converting this into an array se ...

React-admin: Reference Input is only showing IDs instead of displaying my fields

I am currently working with a Reference input in react admin, but I am facing an issue where it displays the ids of the resource instead of the field I specified (status) and I am unsure why this is happening. Here is a snippet of my code: const Candidatur ...

Avoid activating automatic save feature in Material UI data grid

After creating a data-grid and attempting to add records, I encountered an issue where pressing the TAB key automatically saved the data when focusing on the save button without manually pressing enter. How can this behavior be prevented so that manual con ...

use angular to dynamically add attributes to html components

When I need to insert attributes into a component that I plan to reuse, I receive different attributes as strings. For example, if I want to add an element in my component: <input type="text" maxlength="10" placeholder="enter your name"/> I will re ...

Can you please identify the TypeScript type associated with the return value of the match() method in String prototype?

I need help with creating a Regex in JavaScript const pattern = /S(\d+)E(\d+)/; // identifying characters between "S" and "D" const result = 'SE01E09'.match(pattern); How should I declare the result variable? I have attempted various ...

The validator is incorrectly diagnosing the input as 'invalid' when in reality it is not

Currently, I am working on creating a text field that must not be empty and should also not start with a specific set of characters (let's say 'test'). For example, I want testxyz or an empty field to be considered invalid, while anything e ...

The @Column('decimal') decorator in TypeOrm with Postgres and NestJs is displaying a string instead of a decimal value

When I have columns with type decimal and I am using query builder for aggregation, I face an issue where the decimal values are returned as strings after executing the query. I am looking for a way to globally set my project to recognize decimal numbers ...

Creating an array of strings using data from a separate array of objects in JavaScript/TypeScript

I want to generate a new array of strings based on an existing array of objects, with each object belonging to the Employee class and having a firstName field: assignees: Array<Employee>; options: string[]; I attempted to achieve this using the fol ...

Discovering the method to access a local function within a static function in Javascript ES6 (ES2015) or Typescript

Is there a way to access the non-static "foo2" method from inside the static "bar" method? So far, I'm only able to access the "foo1" and "foo3" methods. Can anyone provide guidance on how to achieve this? let foo1 = () => { alert('foo1†...

Is it feasible to develop a TypeScript module in npm that serves as a dual-purpose tool, functioning as both a command line utility

My goal is to develop an npm TypeScript module that serves dual purposes - acting as a command line utility and exporting methods for use in other modules. The issue arises when the module intended for use as a command line utility requires a node shebang ...

Amazon has raised concerns about my use of an incorrect algorithm for signing requests

Need to include the string "AWS4" in my code implementation, which involves Angular and Python. In Python, I calculate the signature and then pass it to the frontend for sending the file to AWS. Here is a snippet of the signature and payload code: signa ...

Is it possible to utilize a single node_modules folder for multiple Angular 2/4 projects?

As I dive into the world of Angular, I've noticed that creating a new project can be time-consuming due to the 100MB "node_modules" folder that the CLI generates. The contents of this folder are repetitively consistent across projects, with few change ...

Issues with concealing the side menu bar in Vue.js

I've been attempting to conceal the side menu bar, with the exception of the hamburger icon when in the "expanded" state. Despite my efforts to modify the CSS code, I am still struggling to hide the small side menu bar. The following images represent ...