简体   繁体   中英

Console.Log(Object Number) - Object of Object

Using Angular2 and Firebase I'm retrieving a news article.

I log my retrieved data and its appearing as an Object with an array of Objects but there is only 1 item in the array starting at 39 .

Can i get the 39 so i can retrieve this object?

Or is there another way?

Thanks

GWS

    this._ReturnsService.fetchDataId(id)
        .subscribe((data) => {
            console.log(data);
            this.news = data[39];
            this.bodyofartical = this.news.body.replace(/\n/g, '<br />');
        })

在此处输入图片说明

您可以执行Object.keys(data)[0]来返回您要查找的39

You can start with following code and expend as needed.

this._ReturnsService.fetchDataId(id)
    .subscribe((data) => {
        console.log(data);
        data.forEach( d => {
            this.bodyofartical = d.news.body.replace(/\n/g, '<br />');
        }
    })

With above code you don't have to worry about index id.

The code will loop through the whole data array.

You will have to modify the loop if there is more than 1 item in data array, else this.bodyofartical will always get the last article in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM