Combine two Observable arrays into a single array and showcase their contents using ngFor within an Ionic 3 application

In my Ionic 3 project, I am fetching two arrays through API calls. My goal is to display the data from both arrays in the same line like this:

[item1.1] [item2.1] 
[item1.2] [item2.2] 
[item1.3] [item2.3]

Instead of displaying them like this:

[item1.1] [item1.2] 
[item1.3] [item2.1] 
[item2.2] [item2.3]

I have experimented with various methods including using forkJoin and combineLatest, but they only show one array fully before moving on to the next.

Observable.combineLatest(this.arr1, this.arr2).subscribe(
    (ValOne, ValTwo) => {
        console.log(ValOne);
        console.log(ValTwo);
    }
);

When I try to display the combined array in HTML, it does not maintain the correct order.

<div ion-item *ngFor="let x of (combinedArr | async)">
    <h2>{{ x.name }}</h2>           
    <h2> {{ x.id }} </h2>
</div>

I also attempted to show the data in two columns of a grid, but faced the same issue with the output.

<ion-grid>
    <ion-row>
        <ion-col *ngFor="let x of (arr1 | async);">
            <div>
                1 of 3 {{x.name}}
            </div>
        </ion-col>
        <ion-col>
            <div *ngFor="let y of (arr2 | async);">
                2 of 3 {{y.id}}
            </div>
        </ion-col>
    </ion-row>
</ion-grid>

Answer №1

Here is a potential solution to achieve the desired outcome:

By using Observable.combineLatest with arrays arr1 and arr2, you can create a new array combinedArr that merges the values of both arrays element by element.

The resulting combinedArr consists of rows, each containing an array representing columns:

<div class="grid-container">
    <ul>
        <li *ngFor="let row of combinedArr;">
            <ul>
                <li *ngFor="let col of row;">
                    <div>
                        Column Value: {{col}}
                    </div>
                </li>
            </ul>
        </li>
    </ul>
</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

What are the benefits of reverting back to an Angular 1 directive instead of using an Angular 2 application

Our team has created numerous Angular 1 components and is eager to incorporate them into our Angular 2 application. However, the documentation suggests that we must downgrade the Angular 2 application in order to integrate and utilize the Angular 1 direc ...

NG0900: Issue encountered while attempting to compare '[object Object]'. Please note that only arrays and iterable objects are permitted for comparison

Experimenting with an Angular project where I am retrieving data from a Minecraft API and displaying it on my website. This is my first time working with Angular's HTTP requests. Encountered the following error code; NG0900: Error trying to diff &apo ...

Encountered CORS error when attempting to access the dynamic menu API after logging

Currently, I am working on an Angular 6 and Codeigniter project. In this project, the slider and navigation menu bar are being fetched dynamically through a REST API. Everything runs smoothly until the login process, where a CORS error is encountered. htt ...

The Angular route successfully navigated to the page, but the HTML content was not

Whenever I select the Home option in the navigation bar, it takes me to the home URL but doesn't display the HTML content. Below is my app.routing.module.ts code: import { Component, NgModule } from '@angular/core'; import { RouterModule, Ro ...

What is the most effective method for comparing and updating objects within arrays or lists in frontend Angular applications using TypeScript?

While I acknowledge that this approach may be effective, I am of the opinion that there exists a superior method to accomplish the same goal I have two separate lists/arrays containing distinct objects, each with a common attribute. My objective is to ite ...

Prepending the emulated prefix to Angular 6-7 ViewEncapsulation

Can we customize the tags generated when using ViewEncapsulation.Emulated in an Angular 2-7 component? Currently, it generates tags like [_ngContent-C0], but is there a way to add a custom string to the generated tag, such as [_ngContent-C0-myApp]? Thank ...

When a URL is entered, information is not retrieved from the database

I have a simple app setup where the data (iPhones from the database) is fetched in the AppComponent itself. ngOnInit(): void { this.iphoneservice.fetchIphones(); } The fetchIphones method is located in my iPhoneService file and consists of 3 functio ...

Building an advanced numerical input validation system with Angular 7

In AngularJS, the following code is used to create a form with an input field of type "number": <form name="f" novalidate> <input type="number" ng-model="x" name="x"> </form> <div ng-if="f.x.$error.number">NaN</div> Is it p ...

Exploring Geographic Navigation with Angular Maps

Currently, I'm implementing Google Maps into my Angular SPA by following this helpful article. The HTML code in app.component.html looks like this: <html> <head> <meta charset="utf-8" /> <title>Map</titl ...

Guide to iterating through an Observable<Object[]> to generate an array of objects

Google Firestore collection named users is structured as follows : { "contactNumber":"0123456789", "email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e2e7e0e6ece7edc8efe5e9e1e4a6ebe ...

Ways to utilize a single node_modules folder across multiple projects

Is there a simple method to centralize and share one node_modules folder among all Angular projects, thereby preventing the need to download the same dependencies each time we start a new project? If so, is this approach recommended (what are the pros and ...

Differentiating body classes in Angular 5

I am working on a project with both a login screen and a dashboard screen. The body classes for these screens are different, as shown below: <body class="hold-transition skin-black-light sidebar-mini" > // for dashboard <body class="hold-transi ...

Comparing tick and flushMicrotasks in Angular fakeAsync testing block

From what I gathered by reading the Angular testing documentation, using the tick() function flushes both macro tasks and micro-task queues within the fakeAsync block. This leads me to believe that calling tick() is equivalent to making additional calls pl ...

Calculate the sum of elements within an array

I've been attempting to calculate the sum of values in an array based on different levels, but so far I haven't had much success. Here are the data I'm working with (stored in a variable called totalByLevel): https://i.stack.imgur.com/rOP9 ...

Utilizing Angular to augment existing items in local storage

Hey everyone, I'm facing an issue with localStorage that I can't seem to figure out. I have a form where the first step collects name and gender, and the second step asks for weight and height. The data from step 1 is saved in localStorage, but ...

In the latest release of Angular2, some routers are lacking a provider

After the recent upgrade from beta to RC1, I've encountered some routing issues. While some routes are functioning properly, others throw the following exception: Error: Uncaught (in promise): EXCEPTION: Error in :0:0 ORIGINAL EXCEPTION: No provider ...

enigmatic issue arising in Angular/Node

Could someone please shed some light on what might be causing the issue in my code? Thank you in advance! webpack: Compilation Failed. My Angular CLI: 1.6.3 Node: 8.9.4 OS: Windows 32-bit Angular: 5.2.1 Error Message: ERROR in ./node_modules/css-loader ...

Encountering issues accessing / Error within MEAN stack application

I recently completed the Heroku tutorial, which you can find here. I followed all the steps but did not deploy to the Heroku server and instead worked on my localhost. However, when I tried to access my application using localhost port 8080, I encountered ...

Resetting the Angular2 poller in an ng-bootstrap accordion

I am currently utilizing a multi-dimensional array connected to a reactive poller that waits for a database state update. Interestingly, when I initially load the state once, the user interface functions as intended. However, a challenge arises when I act ...

Custom styles for PrimeNG data tables

Struggling to style the header of a datatable in my Angular project using PrimeNG components. Despite trying various solutions, I am unable to override the existing styles. Even attempting to implement the solution from this PrimeNG CSS styling question o ...