0

my angular application will recive object's from backend

{
    "count": 6,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 160,
            "title": null,
            "rate": 5,
            "author": "anonymous",
            "content": null,
            "review": "here is the review",
            "url": "https://blog.com/?p=198",
            "fake": true,
            "tags": [
                "thi"
            ],
            "comments": [],
            "created_at": "2020-06-13T20:24:32.434131Z",
            "advertisement": {
                "id": 55,
                "created_at": "2020-06-13T20:24:32.432084Z",
                "title": null,
                "url": null,
                "advertizing_content": null
            }
        },

JSON data structure from backend i have converted array

var dat:any=JSON.stringify(backenddata);
     this.z = backenddata["results"][0];

and seleted results

 {
                "id": 160,
                "title": null,
                "rate": 5,
                "author": "anonymous",
                "content": null,
                "review": "here is the review",
                "url": "https://blog.com/?p=198",
                "fake": true,
                "tags": [
                    "thi"
                ],
                "comments": [],
                "created_at": "2020-06-13T20:24:32.434131Z",
                "advertisement": {
                    "id": 55,
                    "created_at": "2020-06-13T20:24:32.432084Z",
                    "title": null,
                    "url": null,
                    "advertizing_content": null
                }
            },

i want to fetch all the tags in every object

console.log(this.z.tags)

i just got "undefined" in console

I tried

Object.values(tags);

i tried everything regarding this objects part objects.values,keys and all when i tried console.log(object.values(y)[8]);(y here is the json object) i'm just getting a value of first object's tag i want to loop all the objects segregate those tags and save it in variable

refereed many stack overflow thread's but its of no use i have no clue what exactly is wrong https://www.youtube.com/watch?v=23ZjKQL-2E4&t=509s

also tried this tutorial still its of no use

i just want to fetch all the tags of every object of thatjson response and save it

1 Answer 1

2

If you need tags from all the objects of the results array, you could use the map function. Try the following

tags = [];

this.someService.getData().subscribe(
  data => {
    this.tags = data['results'].map(result => result['tags']);
    // this.tags = [['thi'], ['thi', 'ths'], ...]
  },
  error => { }
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.