Iterate over object in Angular

前端 未结 17 1336
攒了一身酷
攒了一身酷 2020-11-22 12:19

I am trying to do some things in Angular 2 Alpha 28, and am having an issue with dictionaries and NgFor.

I have an interface in TypeScript looking like this:

相关标签:
17条回答
  • 2020-11-22 12:42

    So I was going to implement my own helper function, objLength(obj), which returns just Object(obj).keys.length. But then when I was adding it to my template *ngIf function, my IDE suggested objectKeys(). I tried it, and it worked. Following it to its declaration, it appears to be offered by lib.es5.d.ts, so there you go!

    Here's how I implemented it (I have a custom object that uses server-side generated keys as an index for files I've uploaded):

            <div *ngIf="fileList !== undefined && objectKeys(fileList).length > 0">
              <h6>Attached Files</h6>
              <table cellpadding="0" cellspacing="0">
                <tr *ngFor="let file of fileList | keyvalue">
                  <td><a href="#">{{file.value['fileName']}}</a></td>
                  <td class="actions">
                    <a title="Delete File" (click)="deleteAFile(file.key);">
                    </a>
                  </td>
                </tr>
              </table>
            </div>
    
    0 讨论(0)
  • 2020-11-22 12:43

    If someone is wondering how to work with multidimensional object, here is the solution.

    lets assume we have following object in service

    getChallenges() {
        var objects = {};
        objects['0'] = { 
            title: 'Angular2', 
            description : "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
        };
    
        objects['1'] = { 
            title: 'AngularJS', 
            description : "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
        };
    
        objects['2'] = { 
            title: 'Bootstrap',
            description : "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
        };
        return objects;
    }
    

    in component add following function

    challenges;
    
    constructor(testService : TestService){
        this.challenges = testService.getChallenges();
    }
    keys() : Array<string> {
        return Object.keys(this.challenges);
    }
    

    finally in view do following

    <div *ngFor="#key of keys();">
        <h4 class="heading">{{challenges[key].title}}</h4>
        <p class="description">{{challenges[key].description}}</p>
    </div>
    
    0 讨论(0)
  • 2020-11-22 12:45

    Define the MapValuesPipe and implement PipeTransform:

    import {Pipe, PipeTransform} from '@angular/core';
    
    @Pipe({name: 'mapValuesPipe'})
    export class MapValuesPipe implements PipeTransform {
        transform(value: any, args?: any[]): Object[] {
            let mArray: 
            value.forEach((key, val) => {
                mArray.push({
                    mKey: key,
                    mValue: val
                });
            });
    
            return mArray;
        }
    }
    

    Add your pipe in your pipes module. This is important if you need to use the same pipe in more than one components:

    @NgModule({
      imports: [
        CommonModule
      ],
      exports: [
        ...
        MapValuesPipe
      ],
      declarations: [..., MapValuesPipe, ...]
    })
    export class PipesAggrModule {}
    

    Then simply use the pipe in your html with *ngFor:

    <tr *ngFor="let attribute of mMap | mapValuesPipe">

    Remember, you will need to declare your PipesModule in the component where you want to use the pipe:

    @NgModule({
      imports: [
        CommonModule,
        PipesAggrModule
      ],
    ...
    }
    export class MyModule {}
    
    0 讨论(0)
  • 2020-11-22 12:47

    try to use this pipe

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({ name: 'values',  pure: false })
    export class ValuesPipe implements PipeTransform {
      transform(value: any, args: any[] = null): any {
        return Object.keys(value).map(key => value[key]);
      }
    }
    
    <div *ngFor="#value of object | values"> </div>
    
    0 讨论(0)
  • 2020-11-22 12:47

    I have been tearing my hair out with trying to parse and use data returned form a JSON query/ api call. Im not sure exactly where i was going wrong, i feel like i have been circling the answer for days, chasing various error codes like:

    "Cannot find 'iterableDiff' pipe supporting object"

    "Generic TYpe Array requires one argument(s)"

    JSON parsing Errors, and im sure others

    Im assuming i just had the wrong combination of fixes.

    So here's a bit of a summary of gotchas and things to look for.

    Firstly check the result of your api calls, your results may be in the form of an object, an array, or an array of objects.

    i wont go into it too much, suffice to say the OP's original Error of not being iterable is generally caused by you trying to iterate an object, not an Array.

    Heres some of my debugging results showing variables of both arrays and objects

    So as we generally would like to iterate over our JSON result we need to ensure it is in the form of an Array. I tried numerous examples, and perhaps knowing what i know now some of those would in fact work, but the approach i went with was indeed to implement a pipe and the code i used was that the posted by t.888

       transform(obj: {[key: string]: any}, arg: string) {
    if (!obj)
            return undefined;
    
    return arg === 'keyval' ?
        Object.keys(obj).map((key) => ({ 'key': key, 'value': obj[key] })) :
      arg === 'key' ?
        Object.keys(obj) :
      arg === 'value' ?
        Object.keys(obj).map(key => obj[key]) :
      null;
    

    Honestly i think one of the things that was getting me was the lack of error handling, by adding the 'return undefined' call i believe we are now allowing for non expected data to be sent to the pipe, which obviously was occurring in my case.

    if you don't want to deal with argument to the pipe (and look i don't think it's necessary in most cases) you can just return the following

           if (!obj)
              return undefined;
           return Object.keys(obj);
    

    Some Notes on creating your pipe and page or component that uses that pipe

    is i was receiving errors about ‘name_of_my_pipe’ not being found

    Use the ‘ionic generate pipe’ command from the CLI to ensure the pipe modules.ts are created and referenced correctly. ensure you add the following to the mypage.module.ts page.

    import { PipesModule } from ‘…/…/pipes/pipes.module’;
    

    (not sure if this changes if you also have your own custom_module, you may also need to add it to the custommodule.module.ts)

    if you used the 'ionic generate page' command to make your page, but decide to use that page as your main page, remember to remove the page reference from app.module.ts (here's another answer i posted dealing with that https://forum.ionicframework.com/t/solved-pipe-not-found-in-custom-component/95179/13?u=dreaser

    In my searching for answers there where a number of ways to display the data in the html file, and i don't understand enough to explain the differences. You may find it better to use one over another in certain scenarios.

                <ion-item *ngFor="let myPost of posts">
                      <img src="https://somwhereOnTheInternet/{{myPost.ImageUrl}}"/>
                      <img src="https://somwhereOnTheInternet/{{posts[myPost].ImageUrl}}"/>
                      <img [src]="'https://somwhereOnTheInternet/' + myPost.ImageUrl" />
                </ion-item>
    

    However what worked that allowed me to display both the value and the key was the following:

        <ion-list>  
          <ion-item *ngFor="let myPost of posts  | name_of_pip:'optional_Str_Varible'">
    
            <h2>Key Value = {{posts[myPost]}} 
    
            <h2>Key Name = {{myPost}} </h2>
    
          </ion-item>
       </ion-list>  
    

    to make the API call it looks like you need to import HttpModule into app.module.ts

    import { HttpModule } from '@angular/http';
     .
     .  
     imports: [
    BrowserModule,
    HttpModule,
    

    and you need Http in the page you make the call from

    import {Http} from '@angular/http';
    

    When making the API call you seem to be able to get to the children data (the objects or arrays within the array) 2 different ways, either seem to work

    either during the call

    this.http.get('https://SomeWebsiteWithAPI').map(res => res.json().anyChildren.OrSubChildren).subscribe(
            myData => {
    

    or when you assign the data to your local variable

    posts: Array<String>;    
    this.posts = myData['anyChildren'];
    

    (not sure if that variable needs to be an Array String, but thats what i have it at now. It may work as a more generic variable)

    And final note, it was not necessary to use the inbuilt JSON library however you may find these 2 calls handy for converting from an object to a string and vica versa

            var stringifiedData = JSON.stringify(this.movies);                  
            console.log("**mResults in Stringify");
            console.log(stringifiedData);
    
            var mResults = JSON.parse(<string>stringifiedData);
            console.log("**mResults in a JSON");
            console.log(mResults);
    

    I hope this compilation of info helps someone out.

    0 讨论(0)
  • 2020-11-22 12:51

    Angular 6.1.0+ Answer

    Use the built-in keyvalue-pipe like this:

    <div *ngFor="let item of myObject | keyvalue">
        Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
    </div>
    

    or like this:

    <div *ngFor="let item of myObject | keyvalue:mySortingFunction">
        Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
    </div>
    

    where mySortingFunction is in your .ts file, for example:

    mySortingFunction = (a, b) => {
      return a.key > b.key ? -1 : 1;
    }
    

    Stackblitz: https://stackblitz.com/edit/angular-iterate-key-value

    You won't need to register this in any module, since Angular pipes work out of the box in any template.

    It also works for Javascript-Maps.

    0 讨论(0)
提交回复
热议问题