Home  >  Forum  >  Angular
Post New Query

Angular- Get value from nested object in JSON



Asked on November 15, 2019
Hello, i would like to ask how can i get the articleList for every category and specifically i need articleList.title
[
    {
        "id"1,
        "name""Development",
        "articleList": [
            {
                "id"35,
                "title""Title 1",
                "content""Test"
            }
        ]
    },
    {
        "id"2,
        "name""News",
        "articleList": [
            {
                "id"25,
                "title""Title 2",
                "content""Test"
            }
        ]
    },
    {
        "id"3,
        "name""Design",
        "articleList": [
            {
                "id"37,
                "title""Title 3",
                "content""Test"
            }
        ]
    },
    {
        "id"4,
        "name""Test",
        "articleList": [
            {
                "id"57,
                "title""Title 4",
                "content""Test"
            }
        ]
    }
]



Replied on November 15, 2019
Try as following:

import { Component, OnInit } from '@angular/core';


const JSON_DATA = [
    {
        "id": 1,
        "name": "Development",
        "articleList": [
            {
                "id": 35,
                "title": "Title 1",
                "content": "Test"
            }
        ]
    },
    {
        "id": 2,
        "name": "News",
        "articleList": [
            {
                "id": 25,
                "title": "Title 2",
                "content": "Test"
            }
        ]
    },
    {
        "id": 3,
        "name": "Design",
        "articleList": [
            {
                "id": 37,
                "title": "Title 3",
                "content": "Test"
            }
        ]
    },
    {
        "id": 4,
        "name": "Test",
        "articleList": [
            {
                "id": 57,
                "title": "Title 4",
                "content": "Test"
            }
        ]
    }
];

@Component({
  selector: 'app-test',
  template: `      
  `
})
export class TestComponent implements OnInit {
ngOnInit() {
  JSON_DATA.forEach(data => {
     data.articleList.forEach(el=> {
       console.log(el.title);
    });
 });
 } 
}


Output in console:

Title 1
Title 2
Title 3
Title 4




Write Answer




©2019 concretepage.com | Privacy Policy | Contact Us