0

I am new to angular 4 ,Here I need to consume API response and set it to the array .

but in my case it returns [object object].

JSON response Format:

{
  "Table": [
    {
      "S_NO": 1,
      "AN_ID": 37,
      "AN_TYPE": "Church",
      "AN_TITLE": "Donation",
      "AN_DATE": "2018-12-05T18:30:00",
      "AN_STATUS": "Active",
      "AN_DESC": "Test"
    }
  ],
  "Table1": [
    {
      "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0914.JPG"
    },
    {
      "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0916.JPG"
    },
    {
      "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0917.JPG"
    },
    {
      "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0919.JPG"
    },
    {
      "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0921.JPG"
    }
  ]
}

app.component.html

for(var i = 0; i <= data; i++){
        this.urls.push(data['Table1'][0]['IMAGES']);
      }

But it returns [object object].Can anyone help me to fix this .

5
  • Looks like your JSON structure is not what you are thinking of, could you post correct structure by typing manually? Commented Dec 9, 2018 at 8:33
  • It&#39;s easier if you console.log in the browser devtool since it looks the same as the image before Commented Dec 9, 2018 at 8:39
  • The Json Response in question is not actually a JSON. Can you please provide a proper JSON, or sample of whatever you are getting as the response Commented Dec 9, 2018 at 8:44
  • please check the updated post @Sachin Gupta Commented Dec 9, 2018 at 8:46
  • Updated post, you didn't show the whole structure where everything was wrapped in a Table1 object Commented Dec 9, 2018 at 8:56

4 Answers 4

1

Use data.Table1.forEach(image => this.urls.push(image.IMAGES)); to get the image URLs

Sign up to request clarification or add additional context in comments.

5 Comments

url : [object Object],[object Object],[object Object],[object Object],[object Object],localhost:49813/Images/Announcements/Church/Donation/…
It returns all the five urls and five times [object object]
Have you mapped the data object to any specific type? How are you accessing the urls ?
If it has the urls (the values from the data), it means that the solution i provided was working fine and the urls may have some data before pushing these values. Can you please verify the same.
Property 'Table1' does not exist on type 'Object'. so I changed it to data['Table1'].forEach(image => this.urls.push(image.IMAGES));
1

It would be better if you used a code rather than an image, but probably it should be enough to use a map method.

const data = JSON.parse(`{"Table":[{"S_NO":1,"AN_ID":37,"AN_TYPE":"Church","AN_TITLE":"Donation","AN_DATE":"2018-12-05T18:30:00","AN_STATUS":"Active","AN_DESC":"Test"}],"Table1":[{"IMAGES":"http://localhost:49813/Images/Announcements/Church/Donation/IMG_0914.JPG"},{"IMAGES":"http://localhost:49813/Images/Announcements/Church/Donation/IMG_0916.JPG"},{"IMAGES":"http://localhost:49813/Images/Announcements/Church/Donation/IMG_0917.JPG"},{"IMAGES":"http://localhost:49813/Images/Announcements/Church/Donation/IMG_0919.JPG"},{"IMAGES":"http://localhost:49813/Images/Announcements/Church/Donation/IMG_0921.JPG"}]}`);

const urls = data.Table1.map(i => i.IMAGES)

Works like a charm and brings this back:

["http://localhost:49813/Images/Announcements/Church/Donation/IMG_0914.JPG", "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0916.JPG", "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0917.JPG", "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0919.JPG", "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0921.JPG"]

3 Comments

Property 'map' does not exist on type 'Object'. @ Property 'map' does not exist on type 'Object'.
It would be easier to help you if you posted your data as a code, not as a image.
Can you update it once more and post actual JSON object?
0

It should be:

this.urls.push(data.Table1[i].IMAGES);

In your loop

2 Comments

got the same url : [object Object],[object Object],[object Object],[object Object],[object Object] @Lucho
What you get from console.log(this.urls[0])?
0
class Test {
    public urls: string[] = [];
    public data: any = {
        "Table": [
            {
                "S_NO": 1,
                "AN_ID": 37,
                "AN_TYPE": "Church",
                "AN_TITLE": "Donation",
                "AN_DATE": "2018-12-05T18:30:00",
                "AN_STATUS": "Active",
                "AN_DESC": "Test"
            }
        ],
        "Table1": [
            { "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0914.JPG" },
            { "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0916.JPG" },
            { "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0917.JPG" },
            { "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0919.JPG" },
            { "IMAGES": "http://localhost:49813/Images/Announcements/Church/Donation/IMG_0921.JPG" }
        ]
    }

    constructor() {
        this.data.Table1.forEach((img: any) => this.urls.push(img.IMAGES));
    }
}

let t: Test = new Test();
console.log(JSON.stringify(t.urls));

Works like a charm for me:

Output > ["http://localhost:49813/Images/Announcements/Church/Donation/IMG_0914.JPG","http://localhost:49813/Images/Announcements/Church/Donation/IMG_0916.JPG","http://localhost:49813/Images/Announcements/Church/Donation/IMG_0917.JPG","http://localhost:49813/Images/Announcements/Church/Donation/IMG_0919.JPG","http://localhost:49813/Images/Announcements/Church/Donation/IMG_0921.JPG"]

you can test it : https://tsfiddle.tsmean.com/fiddle/MYer0EbO

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.