简体   繁体   English

Console.Log(对象编号)-对象的对象

[英]Console.Log(Object Number) - Object of Object

Using Angular2 and Firebase I'm retrieving a news article. 我正在使用Angular2和Firebase检索新闻文章。

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 . 我记录了检索到的数据,并将其显示为带有对象数组的对象,但是数组中只有一项从39开始。

Can i get the 39 so i can retrieve this object? 我可以拿39来找回这个物体吗?

Or is there another way? 还是有另一种方法?

Thanks 谢谢

GWS 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. 使用上面的代码,您不必担心索引ID。

The code will loop through the whole data array. 该代码将遍历整个data数组。

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. 如果data数组中有多个项目,则必须修改循环,否则this.bodyofartical将始终获取数组中的最后一篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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