Unable to loop through using ngFor

I have a component that retrieves data from the back-end and groups it accordingly.

Below is the code snippet:

 getRecruitmentAgencyClientPositions(): void {
    this._recruitmentAgencyClientsService.getRecruitmentAgencyClientPositions(this.recruitmentAgencyClientId).subscribe(r => {
        this.groupedArray = _.groupBy(r.items, 'groupIdentifier');
        console.log(this.groupedArray);
    });
}

This is an example of how I formatted the data:

 {
  "57a5bcdf-fdf0-494a-ba94-8c50ebb716cf": [
    {
      "groupIdentifier": "57a5bcdf-fdf0-494a-ba94-8c50ebb716cf",
      "salary": 100,
      "commission": 5,
      "recruitmentAgencyClientId": 10,
      "isDeleted": false,
      "deleterUserId": null,
      "lastModifierUserId": null,
      "creationTime": "2020-11-27T18:00:48+02:00",
      "creatorUserId": 3,
      "id": 5
    }
  ],
  "00000000-0000-0000-0000-000000000000": [
    {...}, 
    ...
  ]
}

I'm attempting to generate tables based on the grouping like this:

<div *ngFor="let item of groupedArray">
<div class="primeng-datatable-container positions-table" [busyIf]="primengTableHelper.isLoading">
    <p-table ... >
       ... table structure ...
    </p-table>
    <div class="primeng-no-data" *ngIf="!primengTableHelper.records">
        {{ 'NoData' | localize }}
    </div>
</div>
</div>

However, I'm encountering an error message:

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

How can I resolve this issue?

Answer №1

It seems that the error message is indicating that ngFor can only iterate over arrays and not objects. To convert an object into an array, you can make use of methods like Object.values() or Object.keys(). In your scenario, since the keys match the property groupIdentifier, using Object.values() would be recommended.

An example to incorporate Object.values() within ngFor could look like this:

*ngFor="let item of Object.values(groupedArray)"

Keep in mind that this will result in iteration over:

[
  [
    {
      "groupIdentifier": "57a5bcdf-fdf0-494a-ba94-8c50ebb716cf",
      "salary": 100,
      ...
    }
  ],
  [
    {
      "groupIdentifier": "00000000-0000-0000-0000-000000000000",
      "salary": 100,
      ...
    },
    ...
  ]
]

This implies that a second level of ngFor will be necessary.

The ideal solution to address this issue would involve correctly mapping values when retrieving them from the backend. For instance:

getRecruitmentAgencyClientPositions(): void { 
    this._recruitmentAgencyClientsService.getRecruitmentAgencyClientPositions(this.recruitmentAgencyClientId).subscribe(r => {
        const groupedObject = _.groupBy(r.items, 'groupIdentifier'); // object of arrays
        const groupedArrays = Object.values(groupedObject); // array of arrays
        this.groupedArray = groupedArrays.reduce((flatArray, current) => flatArray.concat(current), []); // flattening arrays for final result
        console.log(this.groupedArray);
    });
}

By doing this, using

*ngFor="let item of groupedArray"
should function smoothly.

UPDATE If you want to iterate per group without flattening the arrays, you can implement it like this:

getRecruitmentAgencyClientPositions(): void { 
    this._recruitmentAgencyClientsService.getRecruitmentAgencyClientPositions(this.recruitmentAgencyClientId).subscribe(r => {
        const groupedObject = _.groupBy(r.items, 'groupIdentifier'); // object of arrays
        this.groupedArray = Object.values(groupedObject); // array of arrays
        console.log(this.groupedArray);
    });
}

In this setup, groupedArray remains as an array where each element represents one group.

Answer №2

It appears that the data in groupedArray is not structured as an array, but rather as an object containing two arrays within it.

To clarify: the first array is: "00000000-0000-0000-0000-000000000000" the second array is: "57a5bcdf-fdf0-494a-ba94-8c50ebb716cf".

If you wish to convert this into a single array for iteration, you can combine the contents of both arrays into one array.

Here's how:

function mergeArrays(array){
    let mergedArray = [];
    array["00000000-0000-0000-0000-000000000000"].forEach(data => {
        mergedArray.push(data);
    });

    array["57a5bcdf-fdf0-494a-ba94-8c50ebb716cf"].forEach(data => {
         mergedArray.push(data)
    });
   return mergedArray;
}

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

There appears to be an issue with Mongoose Unique not functioning properly, as it is allowing

Below is the complete code snippet I am using to validate user data: import { Schema, model } from 'mongoose'; import { User } from './user.interface'; const userSchema = new Schema<User>({ id: { type: Number, required: ...

Error: npm encountered a loop error while attempting to download

Looking to implement Google login, I attempted the following command: npm install --save angularx-social-login. Unfortunately, it returned an error: D:\proj>npm install --save angularx-social-login npm ERR! code ELOOP npm ERR! syscall open npm ERR ...

What factors determine when Angular automatically triggers a setTimeout function compared to another?

Sorry if this all seems a bit odd, but I'll do my best to explain the situation. We have been given access to a small service that provides a simple form UI which we collect results from using an event. We've successfully implemented this in two ...

Having trouble getting webpack and babel to start using npm

Greetings, wonderful people of the internet! I am a newcomer to the enchanting world of programming and I am facing a perplexing issue. Although Webpack is trying to guide me towards the solution, I seem to be struggling with fixing it on my own. const pa ...

Update the HighChart Pie chart depending on the selection in the dropdown menu

Currently, I am working on creating a pie chart using data retrieved from a web socket in JSON format. Once the JSON object is returned, if the user selects the "Pie Chart" option, another Select dropdown will be displayed to choose a specific time period. ...

The issue of circular dependencies in TypeScript arises specifically within the Record type rather than in an ordinary object type

Can you explain the difference between two types, where one throws a TS error and the other does not? type ScopeItem = | string | { all: string; team: string; }; type ScopesTree = Record<string, ScopeItem | Record& ...

The Firebase JQuery .on method is incrementally updating individual values in an array instead of updating them all simultaneously

I am trying to update the values of the orders placed by users on the Corporate's page without a refresh. I have implemented the jQuery .on method for this purpose. However, the values are being returned one by one from the array created for the order ...

Locating the Active Object's Coordinates in fabric.js

When using fabric js, I have successfully drawn various shapes like circles and rectangles. However, I encountered an issue while trying to obtain the coordinates of the rectangle using the fabric.rect() method. canvas.getActiveObject().get('points& ...

React's setState function failed to update the specified value within the set

In attempting to update the state values, I encountered an issue where the state did not get updated as expected. To troubleshoot, I included console logs at each line of code. handleFilter=(event)=> { console.log(this.state.answerStatus) // In ...

Angular: The Ultimate Guide to Reloading a Specific Section of HTML (Form/Div/Table)

On my create operation page, I have a form with two fields. When I reload the page using window.reload in code, I can see updates in the form. However, I want to trigger a refresh on the form by clicking a button. I need help writing a function that can r ...

Unable to retrieve object property-Attempting to access properties of undefined object

Can you help me figure out why I am unable to access the districts property in regions object? const regions = [ { region: "Hlavní město Praha", districts: "Benešov, Beroun, Kladno, Kolín, Kutná Hora, Mělník, Mladá Boleslav, Nymbur ...

Testing the ControllerAs syntax when dealing with forms in AngularJS

I am currently facing a situation with Angular that has left me quite puzzled. Controller function Controller () { // form used in template this.form ... } Template containing a form and utilizing the above controller Template <div ng-contr ...

Eliminate lower values in select 1 if the value selected in select 2 is higher

I am struggling with a particular piece of code and could really use some assistance. Within my project, I have two select boxes. The first box allows users to select the "from" year, while the second box is for selecting the "to" year. What I'm tryi ...

Insert text into the cursor location within Summernote using JQuery

Currently, I am working on a flutter application where I have implemented the summernote editor using JQuery. ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); String txtIsi = data.text .replaceAll("'", '\&bsol ...

When utilizing Jest, the issue arises that `uuid` is not recognized as

My current setup is as follows: // uuid-handler.ts import { v4 as uuidV4 } from 'uuid'; const generateUuid: () => string = uuidV4; export { generateUuid }; // uuid-handler.spec.ts import { generateUuid } from './uuid-handler'; de ...

Is it possible to insert an image directly into the <img> tag without having to first send it to the server?

I've created an upload form that allows users to submit images: <form> <input accept="image/jpeg, image/gif, image/png" type="file" name="image" id="image" class="UploadInput" onchange="submitImageUploaderForm()"/> </form> Once t ...

What is the best way to determine the height of a DIV element set to "auto"?

When setting a fixed height on a div using jQuery, such as $('div').height(200);, the value of $('div').height() will always be 200. This remains true even if the content within the div exceeds that height and overflow is hidden. Is th ...

Ways to verify if the flash plugin has been disabled in the Chrome browser

Is there a way to use JavaScript or jQuery to detect if the flash plugin is blocked in Google Chrome? You can check for a disabled flash plugin using the following code: ((typeof navigator.plugins != "undefined" && typeof navigator.plugins["Shock ...

I am encountering an issue where I am unable to successfully fetch a cookie from the Express backend to the React

const express = require("express"); // const storiesRouter = require("./routes/storiesRouter") // const postsRouter = require("./routes/postsRouter"); // const usersRouter = require("./routes/usersRouter"); const cors = require("cors"); const cookieParser ...

Exploring the depths of nested object arrays

I'm really struggling to complete a FullStackOpen exercise that requires rendering data to the page. With the following code, I need to display the number of Parts each Course has and also reduce the number of Exercises/Parts to show the total exercis ...