0

I have json result like this

{
   "modelList":["modelName1","modelName2"],
   "modelNamesList":["modelName1","modelName2"],
   "yearList":["2021","2020","2019","2018"]
}

I need only years list from above object so I am not getting how to do that, can anyone help me, thank you

I have done something like this

home.component.ts

 export class HomeComponent implements OnInit {

    catlogData: any ={
                "modelList":["modelName1","modelName2"],
                "modelNamesList":["modelName1","modelName2"],
                "yearList":["2021","2020","2019","2018"]
    };


    ngOnInit(): void {}

home.component.html

<ul *ngFor="let a of catlogData">
    <li>year:{{a.yearList[i]}}
    </li>
</ul>
  • Does this answer your question? [How to iterate object keys using \*ngFor](https://stackoverflow.com/questions/41396435/how-to-iterate-object-keys-using-ngfor) – Naren Jan 19 '21 at 18:31
  • You can apply ```*ngFor``` on ```catlogData.yearList```, you can access directly json property with ```.```. – Abhishek Kumar Pandey Jan 19 '21 at 18:33

1 Answers1

2

You can directly access the yearsList from catlogData since it's an Object(catlogData.yearsList). Incase if you want iterate whole catlogData object, Check this answer

Try like this

 <div>
   <ul *ngFor="let year of catlogData.yearList">
     <li>years: {{ year }}</li>
   </ul>
 </div
Naren
  • 4,152
  • 3
  • 17
  • 28