Angular2 - *ngFor / loop through json object with array

后端 未结 1 1623
旧时难觅i
旧时难觅i 2020-11-30 15:03

Want to loop through json object

//Defined on component side :
jsonObj = {
    \'1\' : [ {\"title\" : \"title1\" , \"desc\" : \"desc1\" }],
    \'2\' : [ {\"         


        
相关标签:
1条回答
  • 2020-11-30 15:30

    So great to find the best solution from Angular itself provides pipe to loop through JSON , and its keyvalue

    <div *ngFor='let item of jsonObj | keyvalue'>
       Key: {{item.key}}
    
        <div *ngFor='let obj of item.value'>
            {{ obj.title }}
            {{ obj.desc }}
        </div>
    
    </div>
    

    WORKIGN DEMO


    Previously :

    Component side :

    objectKeys = Object.keys;
    

    Template side :

    <div *ngFor='let key of objectKeys(jsonObj)'>
       Key: {{key}}
    
        <div *ngFor='let obj of jsonObj[key]'>
            {{ obj.title }}
            {{ obj.desc }}
        </div>
    
    </div>
    

    WORKING DEMO

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