What is the best way to iterate over objects within an array in Ionic version 3?

In my array of objects, each object contains another object. I was able to retrieve the value of the supplier's name using the following loop, but it only retrieves the value from one object. I am looking for a way to obtain the supplier's name from all the objects in the array.

this.storage.get("products").then((data)=>{

 this.inputRowValues = data[0];
 for(let i = 0; i <= data.length; i++){
  this.user = this.inputRowValues[0]['species'].id
  this.supplier =this.inputRowValues[0]['user'].supplierName

 }

0: Object { id: 1559565693469, user: {…}, species: {…} }
​ 1: Object { id: 1559565830396, noofboxes: "1", price: "1", … }
​ 2: Object { id: 1559565855919, user: {…}, species: {…} }

Answer №1

Within your code, make sure to utilize the variable i to iterate through the array within the loop. Otherwise, only the first object will be processed. It is crucial to note that the loop's termination condition should be < rather than <=. Additionally, in order to store all users and suppliers, both this.user and this.supplier should be arrays.

For your consideration, here is the proposed correction (assuming I have correctly understood your issue):

this.storage.get("products").then((data)=>{

for(let i = 0; i < data.length; i++){
  this.user.append(data[i]['species'].id)
  this.supplier.append(data[i]['user'].supplierName)

}

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

Ways to access a nested property within an array

I'm having an issue when trying to access a sub property of an array. Here's the snippet in question: ngOnInit() { this.menus = this.navService.defaultMenu; console.log(this.getMenusItem()); this.registerChangeInProjects(); } T ...

Extracting the name of a track from the /r/listenToThis subreddit for an IFTTT automation script

I have a list of songs gathered from this subreddit, presented in the following format: [ "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)", "Julia Jacklin - Pressure to Party [Rock] (2019)", "The Homeless Gospel Choir - I'm Going Home [Folk ...

Join the Observable and formControl in Angular 4 by subscribing

My goal is to display the data retrieved from FireStore in the screen fields upon loading. However, the buildForm() function is being called before subscribing to the data, resulting in the failure to populate the screen fields with the FireStore data. pe ...

Checkbox in Angular FormGroup not triggering touched state

There seems to be an issue with the Angular form when checking if the form is touched, especially in relation to a checkbox element. Despite the value of the checkbox changing on click, I am seeing !newDeviceGroup.touched = true. I'm not quite sure wh ...

Angular - Ensuring correct rendering of a subcomponent with input parameter on the first update

Here is a snippet of code showcasing a list of educations and a component: <cdk-virtual-scroll-viewport itemSize="5" class="list-scroll"> <app-education-item *ngFor="let education of loadedEducations" ...

Why does the custom method only trigger once with the addEventListener?

I am attempting to connect the "oninput" event of an input range element to a custom method defined in a corresponding typescript file. Here is the HTML element: <input type="range" id='motivation-grade' value="3" min="1" max="5"> This i ...

No output when using Typescript 2.0

Recently, I've been working on a project in VS 2015 update 3 and just integrated Typescript 2.0. Initially, I encountered a lot of errors and had to go through a trial and error process to resolve them. Now, all the errors have been fixed but I' ...

Exploring the Angular 2 ngFor Directive's Impact on View Lifecycle Triggers

Is there a way to trigger an animation after updating an array of values that is bound to the template HTML using *ngFor? In order for my animation to be meaningful, it needs to be triggered after the view has been updated with the most current values. I ...

Code error TS2345 occurs when assigning the argument of type '{ headers: HttpHeaders; }' to a parameter of type 'RequestOptionsArgs'. This indicates a mismatch in the type of data being passed, causing an

Upon running ionic serve, these are the results that I am encountering. My setup consists of Ionic4 version with Angular 8. While executing the command, this error appears: src/app/home/home.page.ts:60:77 - error TS2345: Argument of type '{ headers ...

How to detach functions in JavaScript while preserving their context?

Can functions in JavaScript be detached while still retaining access to their context? For instance, let's say we have an instance of ViewportScroller called vc. We can retrieve the current scroll position with the following method: vc.getScrollPosi ...

What is causing this issue in TypeScript version 4.8?

After updating to TypeScript 4.8 in VSCode, I have encountered an error in one of my React projects that was not present before. Strangely, this error does not prevent the code from compiling successfully when building the project. It's challenging to ...

Error in Typescript SPFx: The property 'news' is not found in the type 'Readonly<{}>'

Currently, I am working on developing a spfx react component to showcase an RSS feed in the browser. My prototype is functional in a test environment, however, as spfx utilizes TypeScript, I am encountering a type error that I am unsure how to resolve. Rs ...

Disable the loader for a specific method that was implemented in the Interceptor

Custom Loader Interceptor A loader has been implemented within an Interceptor. I have a specific requirement where the loader should not be triggered during the upload() function. It should not be applied to that particular method only. ...

Error in TypeScript when accessing object using string variable as index

Currently, I am facing a challenge in my project where I am dynamically generating routes and managing them in an Elysia(~Express) application. The issue arises when TypeScript's type checking system fails to index an object using a string variable. S ...

Using Typescript types or a linter can help avoid mistakenly rendering the raw function in JSX instead of its executed return value

Seeking a resolution to prevent the recurring mistake I often make, as shown below: export function scratch_1 (props: scratch_1Props): ReactElement | null { function renderA (): string { return "A"; } function renderB (): string { ...

Mismatched non-intersecting categories with TypeScript

I have an object that I need to conditionally render some JSX based on certain properties. I want to restrict access to specific parts of the object until certain conditions are met. Here is my scenario: const { alpha, bravo } = myObject; if (alpha.loadin ...

TypeScript is still throwing an error even after verifying with the hasOwnProperty

There exists a type similar to the following: export type PathType = | LivingstoneSouthernWhiteFacedOwl | ArakGroundhog | HubsCampaigns | HubsCampaignsItemID | HubsAlgos | HubsAlgosItemID | TartuGecko | HammerfestPonies | TrapaniSnowLeop ...

I am seeking advice on how to create an extension for a generic class in TypeScript specifically as a getter

Recently, I discovered how to create extensions in TypeScript: interface Array<T> { lastIndex(): number } Array.prototype.lastIndex = function (): number { return this.length - 1 } Now, I want to figure out how to make a getter from it. For exam ...

The image within the ion-slide-box remains hidden until the page is resized

I am facing a very peculiar issue. There is an ion-slide-box containing some images: <ion-view > <ion-content overflow-scroll="false" > <ion-slide-box show-pager="true"> <ion-slide ng-repeat="item in featured"> ...

Switching from callback to function in TypeScript

Currently, I am utilizing the mongodb driver to establish a connection with mongo: public listUsers(filterSurname?:string):any { if (this.connected) { debug.log(this.db); var results; this.db.collection(' ...